Created
March 27, 2013 16:27
-
-
Save dmgarland/5255678 to your computer and use it in GitHub Desktop.
Here is my example...
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
require 'rubygems' | |
require 'pry' | |
class Animal | |
def speak | |
puts "hello" | |
end | |
def self.create_animal(type) | |
case type | |
when "Dog" | |
Dog.new | |
when "Fish" | |
Fish.new | |
when "Owl" | |
Owl.new | |
else | |
nil | |
end | |
end | |
end | |
class Dog < Animal | |
attr_accessor :name | |
attr_accessor :age | |
# def initialize(name, age = 0) | |
# self.name = name | |
# self.age = age | |
# end | |
def speak | |
puts "woof" | |
end | |
def dog_years | |
self.age * 7 | |
end | |
def self.family | |
"Canidae" | |
end | |
def self.new_with_name(name) | |
Dog.new(name) | |
end | |
def self.new_with_name_and_age(name) | |
Dog.new(name, 10) | |
end | |
end | |
# dog = Animal.create_animal("Dog") | |
# puts Dog.family | |
# dog = Dog.new_with_name("Buddy") | |
# dog = Dog.new_with_name_and_age("Buddy") | |
# bentley = Dog.new("Bentley", 5) | |
# puts "Hey #{bentley.name} #{bentley.dog_years}" | |
# izzy = Dog.new("Izzy", 9) | |
# puts "Hey #{izzy.name}" | |
class Fish < Animal | |
attr_accessor :colour | |
def speak | |
puts "wibblewibblewibble" | |
end | |
end | |
# bert = Fish.new | |
# bert.name = "Bert" | |
# bert.colour = "Orange" | |
# puts "Hey #{bert.name}, you're #{bert.colour}" | |
# ernie = Fish.new | |
# ernie.name = "Ernie" | |
# ernie.colour = "Orange" | |
# puts "Hey #{ernie.name}, you're #{ernie.colour}" | |
class Owl < Animal | |
attr_accessor :head_rotation | |
def speak | |
puts "whoooo-ooo, whoooo-ooo" | |
end | |
end | |
# harry = Owl.new | |
# harry.name = "Harry" | |
# harry.head_rotation = 180 | |
# puts "Hey #{harry.name}, your head is #{harry.head_rotation} degrees" | |
print "What animal do you want to create? " | |
animal = Animal.create_animal(gets.strip.chomp) | |
unless animal.nil? | |
puts animal.speak | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment