Skip to content

Instantly share code, notes, and snippets.

@lmduc
Last active September 3, 2019 14:24
Show Gist options
  • Save lmduc/5922c0d9ce2d1cc0b325413489dc799f to your computer and use it in GitHub Desktop.
Save lmduc/5922c0d9ce2d1cc0b325413489dc799f to your computer and use it in GitHub Desktop.
Ruby self and klass

Ruby's self and klass explanation

References

  1. https://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/

  2. https://blog.yugui.jp/entry/846

Important notes

  1. When invoking a method without an explicit receiver, self is the receiver

  2. The klass, a.k.a default definee, is syntactically the outer class in a normal method body

  3. When define a method:

  • If there's an explicit receiver, the method is added to the eigenclass of the receiver
  • If there's no explicit receiver, the default definee will have the method as an instance method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment