I have an array of hashes, and the values are all float numbers.
What are some good ways in Ruby to convert them all to int?
I have this way right now but wonder what other methods there are that are more elegant or clear:
analytics.map {|e| e.keys.each {|k| e[k] = e[k].to_i}; e}
Update: this is a run of the code:
> @analytics = [{:a => 1.1, :b => 123.456}, {'c' => 765.432}]
=> [{:a=>1.1, :b=>123.456}, {"c"=>765.432}]
> @analytics.map {|e| e.keys.each {|k| e[k] = e[k].to_i}; e}
=> [{:a=>1, :b=>123}, {"c"=>765}]
> @analytics
=> [{:a=>1, :b=>123}, {"c"=>765}]
Looks kind of neat.
In general, whenever I find myself juggling several nested iterators, I try to refactor my object model such that I’m actually dealing with actual objects, not just a twisted maze of nested arrays and hashes. This seems to be right on the fence.
After all, Ruby is an object-oriented programming language, not a hash-oriented one.