Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Last active January 21, 2017 13:55
Show Gist options
  • Save Phrogz/3c32d028d6530e280109 to your computer and use it in GitHub Desktop.
Save Phrogz/3c32d028d6530e280109 to your computer and use it in GitHub Desktop.
Showing that a sub-class need not have an #initialize method
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
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