Skip to content

Instantly share code, notes, and snippets.

@GrahamSM
Created May 3, 2016 16:10
Show Gist options
  • Save GrahamSM/395a2175d0252cd81c79a3ad39dc75fe to your computer and use it in GitHub Desktop.
Save GrahamSM/395a2175d0252cd81c79a3ad39dc75fe to your computer and use it in GitHub Desktop.
There are two distinct cases that I will refer to in order to explain the behaviour of 'self' in ruby
First is within class 'instances'. When self is called within a class instance (instance of an object of said class), self is
in this case referring to that particular instance.
For example:
def my_method
p self
end
my_object = Object_Class.new
my_object.my_method
In the above example, my_method would print the object my_object to the console.
Case two is outside the scope of an class instance. In this situation, self is referring to the class itself. For example, if we were to reference
self inside of a class method (method prefixed by self), or outside the scope of any method definitions, we would be referencing
the class itself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment