ruby - Finding if class method is called externally or internally -


class myparent   def self.foo     if this_method_was_called_internally?       puts "yay"      else       puts "boo"     end   end end  class mylibrary < myparent   foo # yay end  mylibrary.foo # boo 

is possible?

the simple answer no. can play caller, gives access call stack, exception backtrace:

def this_method_was_called_internally?   caller[1].include?(...) end 

(caller[1] previous call, i.e. method calling this_method...)

it's hackish, , information caller may not enough.

please don't use other experiment.


Comments