What does self refer to in different contexts within a Ruby program?

Last updated 27 June 2026

  • self is 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, self is main, a special instance of Object.

  • Calling a setter requires self.attr = x, because a bare attr = x creates 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
All Ruby interview questions