Skip to content

Instantly share code, notes, and snippets.

@agarie
Created September 29, 2012 05:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agarie/3803259 to your computer and use it in GitHub Desktop.
Save agarie/3803259 to your computer and use it in GitHub Desktop.
Example of `yield self`
class Pokemon
def battle
yield self
end
end
mewtwo = Pokemon.new
mewtwo.battle do |m2|
puts m2
puts m2.class
end
# => #<Pokemon:0x007f8b74080f28>
# => Pokemon
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