Ruby dynamic variable name

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] # => {}
...

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)