Probably more like trying to throw one with your appendix, but just so you know how to do it, here's an example from the documentation:
Originally Posted by bivouac
You should be able to call instance_variable_set from inside Fred, like this:
def initialize(p1, p2)
@a, @b = p1, p2
fred = Fred.new('cat', 99)
fred.instance_variable_set(:@a, 'dog') #=> "dog"
fred.instance_variable_set(:@c, 'cat') #=> "cat"
fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
You say you think it should be easy - what does the code look like in a language you are more familiar with?
What you are doing is setting the [b]name[b] of a variable dynamically. That's quite unusual. Normally you change the values of variables, not what the variables are themselves. If you want to set a key (a name) and a value at the same time, a hash is much more common. For example:
That way you still know what the name of the variable is: @params. If you set the name of the variable dynamically, it is hard to find the variable again, because you don't know what it is called!
def add_a_key_and_a_value(key, value)
@params[key] = value
As an aside, getAddress is a bit foreign in Ruby. CamelCase is only really used in class and module names, where the case of the first letter is important. So if you wanted to use two words, it would be get_address.
Using "get" at the start of a method name is a little unusual too. Ruby supports this syntax for getters and setters, so an explicit "get" or "set" isn't needed:
So normally you would use something more descriptive than "get" if you needed something special.
@foo = val