Why Do Ruby Setters Need "Self." Qualification Within the Class

Why do Ruby setters need self. qualification within the class?

The important thing to remember here is that Ruby methods can be (un)defined at any point, so to intelligently resolve the ambiguity, every assignment would need to run code to check whether there is a method with the assigned-to name at the time of assignment.

Why is 'self' required to call a Ruby method from within its constructor?

Ruby doesn't know if you want to make a local variable named "name" or use the member variable.

self.name tells it you want to use the member variable.

There isn't any syntax for saying you want to create a local variable, so that has to be the default.

(Also, member variables aren't real, but you know what I mean)

Why is 'self' required to call a Ruby method from within its constructor?

Ruby doesn't know if you want to make a local variable named "name" or use the member variable.

self.name tells it you want to use the member variable.

There isn't any syntax for saying you want to create a local variable, so that has to be the default.

(Also, member variables aren't real, but you know what I mean)

When to use 'self' in Ruby

Whenever you want to invoke a setter method on self, you have to write self.foo = bar. If you just write foo = bar, the ruby parser recognizes that as a variable assignment and thinks of foo as a local variable from now on. For the parser to realize, that you want to invoke a setter method, and not assign a local variable, you have to write obj.foo = bar, so if the object is self, self.foo = bar

Why do I need to use self when assigning values in my Rails models?

Because if you do status = 'open' you are creating a new local variable called status. That's not about Rails. That is about how Ruby interpreter evaluate your code.

Explicit use of `self` in method name in class definition

Because you want to define accessors for all instances of a class; you don't want to define them for certain instances and not define them for other instances. Hence, defining accessors is something you want to do against a class, not an instance; thus accessor has to be a class method, not an instance method. It will be called in the class body when used.

Ruby - instance methods: why can i use getter without self, but setter only with self

Because you are assigning value to local variable in_progress in a function, not the instance variable. The getter works because Ruby will query the local namespace of the start! function for in_progress, it will not find it and then it will query the instance namespace and it will find a method called in_progress and will call it.

There is no way Ruby interpreter can find out whether you want to assign the true value in a local in_progress or on the instance variable, so the rule is to assign it locally (to the current namespace in start!).

Why do private setters behave differently to other private methods?

Private setters are special because otherwise they couldn't be called at all:

foo = :bar

Assigns to the local variable foo, it doesn't send the message foo=.

Note that setters aren't the only things which cannot be called without an explicit receiver. +@, -@, !, ~, [], []=, +, -, *, /, %, &, |, ^, **, <<, >>, ==, ===, =~, !~, !=, <, >, <=, >=, <=> and probably some others I forgot also cannot be called without an explicit receiver. And then there's stuff like +=, <<= and so on.

There need to be exceptions for all of those. Unfortunately, there are only exceptions for some of those.

It has been proposed to change the rules for private from

Can only be called without an explicit receiver, except for [this long list of exceptions which is really complicated, and still not complete].

to

Can only be called without an explicit receiver or the literal special variable self as receiver.

which preserves all the current properties of the definition (the most important one being that it can be determined statically at parse time).

But so far, nothing has come of it.

Calling attribute accessor methods from within the class

You're doing nothing "wrong" per se. Ruby just thinks you intend to set the local variable test to val, not call the test= method. self.test = val will do what you expect.



Related Topics



Leave a reply



Submit