Last active
January 21, 2017 13:55
-
-
Save Phrogz/3c32d028d6530e280109 to your computer and use it in GitHub Desktop.
Showing that a sub-class need not have an #initialize method
This file contains hidden or 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 Person | |
attr_reader :firstname, :lastname | |
def initialize( first, last ) | |
@firstname, @lastname = first, last | |
end | |
def say_hi | |
puts "Hello, my name is #{@firstname} #{@lastname}" | |
end | |
end | |
class Child < Person | |
def say_hi | |
puts "HI I'M #{@firstname.upcase} I LIKE TURTLES" | |
end | |
end | |
dad = Person.new "Robert", "Johnson" | |
kid = Child.new "Emily", "Johnson" | |
dad.say_hi #=> Hello, my name is Robert Johnson | |
kid.say_hi #=> HI I'M EMILY I LIKE TURTLES |
This file contains hidden or 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 Person | |
attr_reader :firstname, :lastname | |
def initialize( first, last ) | |
@firstname, @lastname = first, last | |
end | |
def say_hi | |
puts "Hello, my name is #{@firstname} #{@lastname}" | |
end | |
end | |
class Child < Person | |
def initialize( firsty, lasty, likey ) | |
super(firsty,lasty) | |
@like = likey | |
end | |
def say_hi | |
puts "HI I'M #{@firstname.upcase} I LIKE #{likey.upcase}" | |
end | |
end | |
dad = Person.new "Robert", "Johnson" | |
kid = Child.new "Emily", "Johnson", "hot dogs" | |
dad.say_hi #=> Hello, my name is Robert Johnson | |
kid.say_hi #=> HI I'M EMILY I LIKE HOT DOGS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment