is there any way to create variables in Ruby with dynamic names?
I\'m reading a file and when I find a string, generates a hash.
e.g.
file =
You can do it with instance variables like
i = 0
file.lines do |l|
l.split do |p|
if p[1] == "InitGame"
instance_variable_set("@Game_#{i += 1}", Hash.new)
end
end
end
but you should use an array as viraptor says. Since you seem to have just a new hash as the value, it can be simply
i = 0
file.lines do |l|
l.split do |p|
if p[1] == "InitGame"
i += 1
end
end
end
Games = Array.new(i){{}}
Games[0] # => {}
Games[1] # => {}
...