So I am trying to create dynamic variables, and then use them again later without having to type out the full name. The test code I made is as follows
players = [1, 2, 3, 4]
players.each |player|
instance_variable_set("@player_#{player}_hand", "foo")
instance_variable_set("@player_#{player}_value", "bar")
}
puts @player_1_hand # => foo
puts @player_3_value # => bar
players.each { |player|
puts "@player_#{player}_hand"
}
# => @player_1_hand
# => @player_2_hand
# => @player_3_hand
# => @player_4_hand
I want to call and/or update these variables dynamically but I don't know how to do that after I initialize them. The documentation I have found has been more confusing than helpful. Can someone explain how to use these correctly?