V
V
Vitaly Liber2015-06-04 14:02:59
Ruby on Rails
Vitaly Liber, 2015-06-04 14:02:59

How can a hash value trim all spaces?

I have this hash hash["price"] = 5 000.
How can I apply .lstrip to the hash value?
I need it to cut the space between five and zeros, because when I load it via csv, it loads only the numbers before the space in the price:integer field.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
tplus, 2015-06-04
@VitalyLiber

hash["price"] = "5 000".sub!(" ", "")
 => "5000"

Don't forget that sub!will change the value in the hash. If you just need to pass a string with the amount, but not change the hash - just "sub".

T
thepry, 2015-06-04
@thepry

hash.values.each(&:lstrip!)
But, since you need to remove all spaces in a string with a number, then
hash.values.each{ |v| v.delete!(' ') }

S
SilentFl, 2015-06-04
@SilentFl

There is a to_money method - either when saving to a hash, do hash["price"]=price.to_money, or when outputting to csv, return hash["price"].to_money

V
vsuhachev, 2015-06-04
@vsuhachev

There is a good gem - attribute_normalizer , I recommend

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question