Created
August 29, 2011 02:06
-
-
Save drteeth/1177607 to your computer and use it in GitHub Desktop.
ruby lambda example
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 Monster | |
| def what_am_i | |
| end | |
| end | |
| class Ghost < Monster | |
| def what_am_i | |
| puts "I'm a ghost." | |
| end | |
| end | |
| class Goblin < Monster | |
| def what_am_i | |
| puts "I'm a Goblin" | |
| end | |
| end | |
| create_a_ghost = lambda { Ghost.new } | |
| create_a_goblin = lambda { Goblin.new } | |
| monster_factories = [create_a_ghost, create_a_goblin] | |
| monster_factory = monster_factories[ rand(monster_factories.length)] | |
| monster = monster_factory.call() | |
| monster.what_am_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment