Because that’s not how “each” works. You cannot call a method on “each”
in order to call it on every element. The “each” method without a block
returns an Enumerator.
So you’ll either have to stick with the solution above or write your own
method. Or you should rethink the structure of your data. A deeply
nested hash or array is almost always bad design and should be replaced
with with an appropriate object (like a table, a tree or whatever fits
best).
By the way, replace “values.each” with “each_value”. This is easier to
read and doesn’t create an array with all values.
module Enumerable
def recursive_each &block
(respond_to?(:each_value) ? each_value : each).each do |v|
if v.respond_to? :recursive_each
v.recursive_each(&block)
else
yield v
end
end
end
end
c = {1=>{“foo”=>[“bar”, “baz”]}}
c.recursive_each do |v|
puts v
end
–
Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: [email protected]
*Origin: Happy Hacking!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.