ruby on rails - Hash is not saving date as values? -
i have hash in ruby 1.8.7 , rails 2.3.8:
important_dates = params[:important_dates] important_dates =#> {"third_test"=>"july 11, 2014", "fourth_test"=>"august 08, 2014", "second_test"=>"june 13, 2014", "sixth_test"=>"august 05, 2014"}
the dates coming calendar_select_date_tag
in rails passing date object, in params getting string when try doing this:
new_dates = important_dates.each_value{|r| r.to_date} new_dates[:fourth_test].class.inspect #=> string
i have been on hours now.
new_dates = hash[important_dates.map{|k,v| [k, v.to_date]}]
or if you're using ruby 2.0 or higher:
new_dates = important_dates.map{|k,v| [k, v.to_date]}.to_h
update: why answer not working:
each_value
returning initial hash iterates on , drops block return values. note, example:
h = {a: 'foo', b: 'bar'} h.each_value {|v| v.upcase } #=> upcase creates new string object h #=> {a: 'foo', b: 'bar'}
however, if value mutable, can try altering during iteration:
h = {a: 'foo', b: 'bar'} h.each_value {|v| v.upcase! } #=> upcase! alters existing string object h #=> {a: 'foo', b: 'bar'}
new_dates = important_dates.each_value{|r| r.to_date}
since want change string object date object, there absolutely no way of doing using each_value
.
Comments
Post a Comment