How to retrieve caller context object in Ruby ?

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-04-10T18:35:57Z Indexed on 2010/04/10 18:43 UTC
Read the original article Hit count: 316

Filed under:
|

Hi, hereafter is my piece of code that I want to simplify in order to avoid passing an extra argument on each call :

module M
  def do_something(context)
    puts "Called from #{context}"
  end
  module_function :do_something
end

class Foo
  def do_stuff
    M.do_something(self)
  end
end

Foo.new.do_stuff

Is there a way to do the same think without passing 'self' as an input argument to 'do_something' method like this ?

module M
  def do_something
    puts "Called from #{method that returns caller object}"
  end
  module_function :do_something
end

class Foo
  def do_stuff
    M.do_something
  end
end

Foo.new.do_stuff

Thanks for your support!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about metaprogramming