| Start |
|---|
| There is always an implicit receiver In Ruby, there is always an implicit receiver for all method calls. The language keeps a reference to the current implicit receiver stored in the variable For example, when you first open In this case the You can define methods on the implicit receiver using the This has defined the method foo on the instance of main object running in your repl. Note that local variables are looked up before method names, so that if you define a local variable with the same name, its reference will supersede the method reference. Continuing from the previous example: The |
| Keywords change the implicit receiver When you define a class or module, the implicit receiver becomes a reference to the class itself. For example: Executing the above code will print: |
| When to use self? Most Ruby code utilizes the implicit receiver, so programmers who are new to Ruby are often confused about when to use 1. To change the receiver. Ordinarily the behavior of 2. To disambiguate the receiver When local variables may have the same name as a method an explicit receiver may be required to disambiguate. Examples: The other common case requiring disambiguation involves methods that end in the equals sign. For instance: |
Read Article
https://develop.horje.com/learn/1434/reference