i’m really confused how to name method names in Ruby classes. if i create an accessor like:
attr_accessor :name
it creates to methods:
name
and
name=
but when i call the second method with a whitespace between the ‘name’ and ‘=’ it works
‘n.name=’ and ‘n.name =’ both works.
i read somewhere that Ruby ignores whitespaces. Well then, why a method written by me does not work when i call it with whitespace?
def getname
end
if i call this way, it doesn’t work. why?
t.get name
i’m not surprised as it does not work. but i’m confused how the setter method (name=) works then?
thanks in advance.
Setters are special in Ruby.
from http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html
Assignments are defined in Ruby as:
from http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html
So
n.name=is calling the settername=directly.n.name =is using this special treatment of setters by the fact that it ends in an=, to make it so that you can use it as the lvalue (that is, it can appear on the left side) in an assignment.