What does self refer to in different contexts within a Ruby program?
Last updated
selfis the current object: the implicit receiver of method calls. It is the instance inside instance methods and the class inside a class body or class method.At the top level,
selfismain, a special instance ofObject.Calling a setter requires
self.attr = x, because a bareattr = xcreates a local variable.
- https://www.honeybadger.io/blog/ruby-self-cheat-sheet/
- https://theleafnode.com/the-value-of-self-in-ruby/
- https://dev.to/sinarezaei/getting-to-know-yourself-understanding-self-in-ruby-8l6
- https://dev.to/vitaliipaprotskyi/what-does-class-self-actually-do-in-ruby-2on1
- https://rubylearning.in/courses/ruby-basics/ruby-self/
- https://medium.com/@jackouni/full-breakdown-of-self-in-ruby-f9210ce0472c
- https://dev.to/souzamartin/self-exploration-self-in-class-and-instance-methods-in-ruby-29i1
- https://dev.to/sonianand11/ruby-interview-questions-part-1-44dp