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?
foo
in the block to see whatfoo
actually is. Also, Ruby documentation is pretty good. Just googling for "ruby hash" then looking up theeach
method, would have put you on the right track. ruby-doc.org/core-2.0/Hash.html#method-i-each – gylaz Jun 8 at 17:21ri Hash
at the command-line would show the various methods.ri Hash.each
would have given more information specific toeach
. – the Tin Man Jun 8 at 18:06