From: "ara.t.howard" Date: 2008-09-18T04:43:40+09:00 Subject: Re: Singleton class's original class On Sep 17, 2008, at 1:23 PM, Paul Brannan wrote: > Given a singleton class: > > irb(main):001:0> class Foo > irb(main):002:1> def self.singleton_class > irb(main):003:2> return class << self; self; end > irb(main):004:2> end > irb(main):005:1> end > => nil > irb(main):006:0> sc = Foo.singleton_class > => # > > How can I get class that the singleton class is a singleton class of? > > (and is there a name for this?) it might not be a class... but will this work? cfp:~ > cat a.rb class Object def singleton_class &block sc = class << self self end unless sc.respond_to?(:parent) parent_id = self.object_id #Module === self ? self : self.class sc.instance_eval "def parent() ObjectSpace._id2ref(#{ parent_id }) end" end block ? sc.module_eval(&block) : sc end end p File.singleton_class.parent p Array.new.singleton_class.parent p Hash.new.singleton_class.parent p Hash.new.singleton_class{ parent } p Hash.singleton_class{ parent } options = { :key => :value } options.singleton_class do def getopt opt fetch(opt.to_s.to_sym) rescue fetch(opt.to_s) end end p options.getopt(:key) cfp:~ > ruby a.rb File [] {} {} Hash :value a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama