From: Todd Benson Date: 2008-01-23T12:19:34+09:00 Subject: Re: Determine where a method is being called from? On Jan 22, 2008 9:00 PM, Todd Benson wrote: > On Jan 22, 2008 4:47 PM, Ben Johnson wrote: > > Is it possible to determine if a public instance method is being called > > from an object or within the class? > > I think you might need to be a little more specific with your > question. A class is also an object. > > irb(main):001:0> Class.ancestors > -> [Class, Module, Object, Kernel] > > I don't think it's possible to grab a method's owner, because methods > can be created in any context on the fly (somebody please correct me > if I'm wrong). So, really what you are probably looking for is the > binding scope. > > Just a thought, > > Todd You can also do... irb(main);001:0> class C irb(main):002:1> def self.foo irb(main):003:2> puts "in class" irb(main):004:2> end irb(main):005:1> def foo irb(main):006:2> puts "in instance" irb(main):007:2> end irb(main):008:1> end => nil irb(main):009:0> c = C.new => # irb(main):010:0> C.method(:foo) => # irb(main):011:0> c.method(:foo) => # Todd