Created
October 3, 2013 15:48
-
-
Save dmgarland/6812073 to your computer and use it in GitHub Desktop.
Here's that stuff about Factory methods in Ruby
This file contains hidden or 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
| 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