Last active
August 29, 2015 13:57
-
-
Save alexhawkins/2300f5454b177c3fe773 to your computer and use it in GitHub Desktop.
Why self?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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