Ruby: Is it possible to set the value of a instance variable where the instance variable is named via a string?

ruby

Solution

Search for “instance_variable” on Object:

some.instance_variable_get(("@thing_%d" % 2).to_sym)
some.instance_variable_set(:@thing_2, 55)

This pattern is referred to as “fondling”; it can be a better idea to explicitly use a Hash or Array if you will be computing keys like this.

Problem

Not sure what this pattern is called, but here is the scenario: ``` class Some #this class has instance variables called @thing_1, @thing_2 etc. end ``` Is there some way to set the value of the instance variable where the instance variable name is created by a string? Something like: ``` i=2 some.('thing_'+i) = 55 #sets the value of some.thing_2 to 55 ```

Original source