From: "Mauricio Fernández" Date: 2004-09-16T02:06:37+09:00 Subject: Re: Singleton method on object via define_method? On Wed, Sep 15, 2004 at 09:49:50PM +0900, Robert Klemme wrote: > > > >> o = Object.new > > > => # > > > >> class << o; class << self; def to_s ; "foo" end end end > > > => nil > > > >> class << o; class << self;to_s end end > > > => "foo" > > > >> class << o; class << self; class << self; end end end > > > => nil > > > >> class << o; class << self; to_s end end > > > => "#" > > > > It's using Module#to_s because the klass of the singleton class is > > no longer itself... Module#to_s ends up calling > > class << o; self.to_s end > > which you defined before. > > Well yes, but the fact remains that there is a singleton we don't know of, > do we? Probably I don't see clearly your point so I'd appreciate it if > you'd elaborate it. It is I who doesn't understand what the above is supposed to prove :) Before creating the singleton class of the singleton of o's singleton class, to_s returned "foo", afterwards it returns "#"... And anyway, nobody forces us to use to_s batsman@tux-chan:/tmp$ cat dfg.rb class Class # only works for singletons of singletons, will always return # true otherwise def has_singleton? m = "__magic_#{Time.new.to_i}_#{rand(100000)}" class_eval do remove_method(m) rescue nil define_method(m) { } begin send m rescue NameError return true ensure remove_method m end end false end end o = Object.new klass = class << o; class << self; self end end p klass.has_singleton? class << klass; end p klass.has_singleton? batsman@tux-chan:/tmp$ ruby dfg.rb false true -- Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com