self
must always be supplied when wishing to use a setter1 on the left-hand side of an assignment, e.g. self.x = val
or self.x += val
2.
self
can be omitted for getters (which are just normal method calls) when there is not a local variable that shadows the method.
Expressions of the form x = val
or x += val
- that is to say, without self
- always assign to the local variable x
. In this case that means a value was being assigned to the local variable email
(not to be confused with self.email
), which was then discarded when the block exited.
1 See What is attr_accessor in Ruby? for how getters/setters are implemented.
2 When ruby sees self.x = y
it treats it like self.__send__(:x=, y)
. That is, the setter (which is just a normal method with a trailing equals in the name) is called magically.