Search code examples
rubyself

Self and proc.call parameter


I know there are already a lot of questions about self, but I just wanted to make sure I've understood it.

def buttonPressed
    @action.call(self)
end
  1. In this code, self refers to @action (because @action is the receiver of the call method). Is that right?

    { songList.start }
    
  2. If the proc object has no parameters like in this case, why give prc.call self as parameter?


Solution

  • I. Outside of a class, self returns main of type Object. Inside a class, it will return the current instance of the class.

    II. In the case of this particular block, there is indeed no need for self to be passed, but passing self means you can also potentially have actions that can access self.