From: ara.t.howard@... Date: 2006-03-31T00:30:07+09:00 Subject: Re: accessing the currently running method On Thu, 30 Mar 2006, Kev Jackson wrote: > Currently I have > > caller[0][/in: `.+(pg_\d{3})/,1] > which adequately returns the current method (given the current convention of > naming the methods something with the page number in them) > > I tried > class Object > def current_method > caller[0][/in: `.+(pg_\d{3})/,1] > end > end > > but calls to this inside a method_missing method produce nil - not sure why, > but obviously I need to study more ruby to understand why that doesn't work. > Including current_method (as defined above) in module Kernel also doesn't do > the trick, but having the caller[0][/in: `.+(pg_\d{3})/,1] inside the > method_missing method works. > > Thanks for pointing me in the direction of a couple of new things you could use metaprogramming to provide this functionality: harp:~ > cat a.rb class Thread attr_accessor 'where' end class Module require 'thread' def referable *methods methods.flatten.each do |m| module_eval <<-code alias_method "__#{ m }__", "#{ m }" def #{ m }(*a, &b) t = Thread.current where = t.where begin t.where = "#{ m }" __#{ m }__(*a, &b) ensure t.where = where end end code end end end class Object def where Thread.current.where end end # # eg # class << self def foo p where bar end def bar p where end referable 'foo', 'bar' end self.foo class C def method_missing(m, *a, &b) p where foo end def foo p where end referable 'method_missing', 'foo' end C.new.bar harp:~ > ruby a.rb "foo" "bar" "method_missing" "foo" regards. -a -- share your knowledge. it's a way to achieve immortality. - h.h. the 14th dali lama