Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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