Skip to content

Instantly share code, notes, and snippets.

@dmgarland
Created October 3, 2013 15:48
Show Gist options
  • Select an option

  • Save dmgarland/6812073 to your computer and use it in GitHub Desktop.

Select an option

Save dmgarland/6812073 to your computer and use it in GitHub Desktop.
Here's that stuff about Factory methods in Ruby
class Mammal
def self.factory(type)
if choice == "Dog"
mammal = Dog.new
elsif choice == "Cat"
mammal = Cat.new
elsif choice == "Whale"
mammal = Whale.new
else
puts "Sorry don't know what that is..."
end
end
end
class Dog < Mammal
end
class Cat < Mammal
end
class Whale < Mammal
end
mammal = nil
puts "What sort of mammal would you like?"
choice = gets.strip.chomp
mammal = Mammal.factory(choice)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment