Skip to content

Instantly share code, notes, and snippets.

@Whoops
Created January 24, 2011 21:23
Show Gist options
  • Save Whoops/793987 to your computer and use it in GitHub Desktop.
Save Whoops/793987 to your computer and use it in GitHub Desktop.
demonstrate inheritance and polymorphism
C:\Users\walton.hoops\Desktop>ruby vehicle.rb
Cruisin in my Car
Cruisin in my Truck
get a long little doggie!
Tow that gosh durn Car
C:\Users\walton.hoops\Desktop>
class Vehicle
def drive
puts "Cruisin in my #{self.class}"
end
end
class Car < Vehicle
end
class Truck < Vehicle
def tow(target)
puts "Tow that gosh durn #{target.class}"
end
end
class Cattle
def self.drive
puts "get a long little doggie!"
end
end
def drive_vehicle(v)
v.drive
end
drive_vehicle(Car.new)
drive_vehicle(Truck.new)
drive_vehicle(Cattle)
Truck.new.tow(Car.new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment