Created
September 29, 2012 05:24
-
-
Save agarie/3803259 to your computer and use it in GitHub Desktop.
Example of `yield self`
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 Pokemon | |
def battle | |
yield self | |
end | |
end | |
mewtwo = Pokemon.new | |
mewtwo.battle do |m2| | |
puts m2 | |
puts m2.class | |
end | |
# => #<Pokemon:0x007f8b74080f28> | |
# => Pokemon |
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 Pokemon | |
def initialize(move) | |
@move = move | |
end | |
def battle | |
yield self | |
end | |
def use_move | |
puts "used #{@move}!" | |
end | |
end | |
mewtwo = Pokemon.new "Psychic" | |
mewtwo.battle do |m2| | |
m2.use_move | |
end | |
# => used Psychic! | |
arceus = Pokemon.new "Judgement" | |
arceus.battle do |arc| | |
arc.use_move | |
end | |
# => used Judgement! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment