Skip to content

Instantly share code, notes, and snippets.

@klustig88
Created August 12, 2013 18:56
Show Gist options
  • Save klustig88/6213908 to your computer and use it in GitHub Desktop.
Save klustig88/6213908 to your computer and use it in GitHub Desktop.
class Person
def self.example_class_method
puts "We're calling an example class method"
puts "'self' is always defined. What is 'self' here? Let's see."
p self
puts "That was self!"
end
def example_instance_method
puts "We're calling an example *instance* method"
puts "'self' is defined here, too, but it means something different."
p self
puts "That was self, again, but see how it's an instance of the class."
end
end
Person.example_class_method
luis = Person.new
luis.example_instance_method
# When Self is called within a class method, self literraly refers to the class it is being called from.
# When self is called within an instance method, self refers to the instance created within the class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment