I'm pretty new to Ruby (I come from a C++ background) and I have an array/hash of objects by their name but I'm having no luck when trying to access their variables. This is my attempt:
class Foo
attr_reader :num
def initialize(num)
@num = num
end
end
foo_list = {}
foo_list["one"] = Foo.new("124")
foo_list["two"] = Foo.new("567")
foo_list.each do |foo|
p "#{foo.num}" # ERROR: undefined method 'num'
end
I'm pretty sure there is an easy way of doing what I need, maybe not even using 'each' but something else?
fooin the block to see whatfooactually is. Also, Ruby documentation is pretty good. Just googling for "ruby hash" then looking up theeachmethod, would have put you on the right track. ruby-doc.org/core-2.0/Hash.html#method-i-eachri Hashat the command-line would show the various methods.ri Hash.eachwould have given more information specific toeach.