charleseff / alias_method_chain_block

apply an alias_method_chain for a block, unapplied once the block is completed.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

class SomeObject
  def do_stuff
    puts "doing stuff"
  end
end

extra_stuff = -> do
  puts "doing extra stuff"
  do_stuff_without_extra_stuff
end


some_object = SomeObject.new

puts "Outside the block:"
some_object.do_stuff
puts

puts "Inside the block:"
alias_method_chain(SomeObject, :do_stuff, :extra_stuff, extra_stuff) do
  some_object.do_stuff
end
puts

puts "Outside the block again:"
some_object.do_stuff





OUTPUT:

Outside the block:
doing stuff

Inside the block:
doing stuff
doing extra stuff

Outside the block again:
doing stuff

About

apply an alias_method_chain for a block, unapplied once the block is completed.