So I have a class like this:
def Word
end
and im looping thru an array like this
array.each do |value|
end
And inside that loop I want to instantiate an object, with a handle of the var
value = Word.new
Im sure there is an easy way to do this – I just dont know what it is!
Thanks!
To assign things to a dynamic variable name, you need to use something like
eval:but check this is what you want – you should avoid using
evalto solve things that really require different data structures, since it’s hard to debug errors created with eval, and can easily cause undesired behaviour. For example, what you might really want is a hash of words and associated objects, for examplewhich won’t pollute your namespace with tons of
Wordobjects.