From: Robert Klemme Date: 2006-11-19T02:20:07+09:00 Subject: Re: How to call a class method via the singleton if it exists On 18.11.2006 18:00, Trans wrote: > Robert Klemme wrote: >> On 18.11.2006 16:56, Trans wrote: >>> I want to call a class level method from the the instance level, but I >>> need it to go through the singeton if it exists. Here is the most basic >>> example: >>> >>> class X >>> def self.f ; "A" ; end >>> def g ; self.class.f ; end >>> end >>> >>> x = X.new >>> x.g #=> "A" >>> >>> def x.f; "B"; end >>> x.g #=> "A" >>> >>> How do I get the last to return "B"? I know I could use >>> '(class<>> a singelton even if doesn't exist, which seems very wasteful. Yet I >>> don't recall any (non-hack) way to detect if a singleton is defined. Is >>> there? >>> >>> It would be nice if there was a reference with which we could call up >>> through the class level like this. >> Two ideas attached. >> >> robert > > thanks robert. you helped clarify that unfortunately I've i mis-stated > the problem, there's a contradiction in my example, what I actually > meant was: > > class X > def self.f ; "A" ; end > def g ; self.class.f ; end > end > > x = X.new > x.g #=> "A" > > # this should be: > class << x > def self.f; "B"; end > end > > x.g #=> "A" # want "B" > > so #respond_to? won't help b/c I didn't intend #f to be instance level > method. sorry. what i'm asking for, should make a whole lot more sense > now (i hope) > > thanks, > T. Just replace self.class with class<