Skip to content

Instantly share code, notes, and snippets.

@data-doge
Created February 16, 2015 06:57
Show Gist options
  • Save data-doge/2714ed9f2ccbb9544143 to your computer and use it in GitHub Desktop.
Save data-doge/2714ed9f2ccbb9544143 to your computer and use it in GitHub Desktop.
drill-the-self-keyword-challenge
## CLASS CONTEXT
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
## Write your driver code to run both methods above here:
Person.example_class_method
person = Person.new
person.example_instance_method
## MODULE CONTEXT
## The following are identical. Comment each out separately and write the driver code to run the hypot method
module Math
def self.hypot(a, b)
puts "a: #{a}, b: #{b}"
end
end
Math.hypot(3,4)
module Math
def Math.hypot(a, b)
puts "a: #{a}, b: #{b}"
end
end
Math.hypot(3,4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment