Skip to content

Instantly share code, notes, and snippets.

@kellishouts
Created January 30, 2015 01:20
Show Gist options
  • Save kellishouts/fc108e1e1f2ab4253d6c to your computer and use it in GitHub Desktop.
Save kellishouts/fc108e1e1f2ab4253d6c to your computer and use it in GitHub Desktop.
ROOP: Ruby OOP Example
// oop.rb
class Animal
# properties
attr_accessor :name, :last_ate
# constructor
def initialize( name )
@name = name
puts "made an Animal named #{name}"
end
# instance method
def eat( food )
@last_ate = food
puts "#{@name} noms a #{food.name}"
end
# intance method
def poop()
puts "#{@name} poops!!!"
end
end
class Dog < Animal
# properties
attr_accessor :breed
# constructor method
def initialize()
super( "unnamed dog" )
end
end
class Cat < Animal
# properties
attr_accessor :color
# constructor
def initialize( name, color )
@color = color
super( name )
end
end
# instantiate new instance of Animal class
animal = Animal.new( "Doug Funny" )
animal.poop()
snoopy = Dog.new
snoopy.breed = "Beagle"
snoopy.name = "Snoopy"
puts snoopy.inspect()
mochi = Cat.new( "Mochi", "grey" )
mochi.eat( snoopy )
mochi.poop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment