When I set or get instance variables using some name, for example @foo, I can do something like:
instance_variable_set("@foo", some_value)
...
instance_variable_get("@foo")
But often, I use a variable for the method name, which does not include the @ prefix, so that I end up doing:
method = :foo
...
instance_variable_set("@#{method}", some_value)
...
instance_variable_get("@#{method}")
But since all instance variables are prefixed with @, I think it redundant to have to type "@#{method}" instead of simply typing method. Why are the methods instance_variable_set and instance_variable_get not designed to accept string/symbol without @ as its first argument like this:
method = :foo
...
instance_variable_set(method, some_value)
...
instance_variable_get(method)
where the variable to be actually set will be @foo rather than foo?
Is there any advantage with the way it is?
instance_variable_(get|set)a code smell?:"hello?"is a valid symbol - is it a valid variable name?@, then there will be no problem. I was wondering why it could not be likeattr_(accessor|reader|writer), where you don't put@.