From: "Mauricio Fernández" Date: 2004-09-16T08:31:47+09:00 Subject: Re: Singleton method on object via define_method? On Thu, Sep 16, 2004 at 07:39:49AM +0900, Robert Klemme wrote: > >>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 :) > > Utter confusion on all sides. :-))) > > My understanding: someone (I?) made a remark about the fact (well, assumed > fact) that you can't test whether an instance has a singleton class. You > can access it, but you will not know whether it was created for this access > or whether it existed before. You then presented the example with a > NameError due to undefined method foo. It's more than a NameError, it's an anomaly in Ruby's object model that I can detect as a *successful* call that should fail. You get the NameError when that anomalous situation is no longer there (pushed one singleton away, so to speak). > I thought, you use the NameError > for detecting the singleton class, hence I used to_s which I know is always But I wouldn't use #to_s for the test *g* > defined and thus would not trigger the exception. The idea being to > disprove the test. Maybe I got something wrong here... That could be detected and a new (random) name would be used. > >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 > > I'm not sure whether I fully understand what the test should do. But it's > late and I'll better go to bed now. A brain with some sleep thinks > better... > > >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 > > Hm, but what does this tell me: > > >>o = Object.new > => # > >>klass = class << o; self end > => #> > >>klass.has_singleton? > => true > > The answer is definitely wrong, because the singleton class of o does not > have a singleton klass. Hm... Hence the warning in my code: # only works for singletons of singletons, will always return # true otherwise that is, it only says the truth when called on singletons of singletons. In the snippet above, you called it on a singleton of a non-singleton. > For the moment I'll stick with my statement that it's generally not > possible to determine whether an instance has a singleton or not other than > by using some kind of extension. :-) We're both right, it is generally impossible, and "there's at least one case where you can know if the singleton class has been created or not (singletons of singletons)". (And you can always know if you're using evil.rb, of course :-) -- Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com