Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhawkins/2300f5454b177c3fe773 to your computer and use it in GitHub Desktop.
Save alexhawkins/2300f5454b177c3fe773 to your computer and use it in GitHub Desktop.
Why self?
class Animal
attr_accessor :name
def initialize(name)
@name = name
end
def eat(other)
puts "#{@name} ate #{other.name}! #{self.noise}"
end
end
class Human < Animal
attr_accessor :catchphrase
def initialize(name, catchphrase)
super(name)
@catchphrase = catchphrase
end
def noise
@catchphrase
end
end
a = Human.new("Adam", "Right on!")
b = Animal.new("Chicken")
a.eat(b)
#=> "Adam ate Chicken! Right on!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment