A
Ara.T.Howard
at the risk of totally annoying everyone on this list and not getting any real
work done for another day i'm posting some of the code i've scraped out of
this week's posts and throwing down a challenge to boot:
jib:~/eg/ruby > cat a.rb
class Class
def singleton
class << self; self; end
end
def singleton?
not self.ancestors.include? self
end
def inside_singleton?
singleton?
end
def singleton_of
if singleton?
m = %r/^(?:#<Class
+(.*?)(>+)$/io.match inspect
singleton_name, brackets = m[1], m[2]
n = brackets.size - 1
klass = klass_stamp singleton_name
n.times{ klass = class << klass; self; end }
klass
else
nil
end
end
def klass_stamp hierachy
relatives = hierachy.split %r/::/
parent = Object
while((child = relatives.shift))
klass = parent.const_get child
parent = klass
end
klass
end
end
module M
class C
p singleton_of # => nil
class << self
p singleton_of # => M::C
class << self
p singleton_of # => #<Class:M::C>
class << self
p singleton_of # => #<Class:#<Class:M::C>>
end
end
end
end
end
#
# we now explode here
#
class << self
p singleton_of
end
jib:~/eg/ruby > ruby a.rb
nil
M::C
#<Class:M::C>
#<Class:#<Class:M::C>>
a.rb:28:in `const_get': wrong constant name #<Object:0xb7447a10 (NameError)
from a.rb:28:in `klass_stamp'
from a.rb:17:in `singleton_of'
from a.rb:54
the challenge is obviously to implement Class#singleton_of without parsing the
output of inspect or, at least so that it works for all cases. the return
value of Class#singleton_of ought to be pretty obivous - it's the thing on the
rhs of
class << any_object
release the hounds!
-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
work done for another day i'm posting some of the code i've scraped out of
this week's posts and throwing down a challenge to boot:
jib:~/eg/ruby > cat a.rb
class Class
def singleton
class << self; self; end
end
def singleton?
not self.ancestors.include? self
end
def inside_singleton?
singleton?
end
def singleton_of
if singleton?
m = %r/^(?:#<Class
singleton_name, brackets = m[1], m[2]
n = brackets.size - 1
klass = klass_stamp singleton_name
n.times{ klass = class << klass; self; end }
klass
else
nil
end
end
def klass_stamp hierachy
relatives = hierachy.split %r/::/
parent = Object
while((child = relatives.shift))
klass = parent.const_get child
parent = klass
end
klass
end
end
module M
class C
p singleton_of # => nil
class << self
p singleton_of # => M::C
class << self
p singleton_of # => #<Class:M::C>
class << self
p singleton_of # => #<Class:#<Class:M::C>>
end
end
end
end
end
#
# we now explode here
#
class << self
p singleton_of
end
jib:~/eg/ruby > ruby a.rb
nil
M::C
#<Class:M::C>
#<Class:#<Class:M::C>>
a.rb:28:in `const_get': wrong constant name #<Object:0xb7447a10 (NameError)
from a.rb:28:in `klass_stamp'
from a.rb:17:in `singleton_of'
from a.rb:54
the challenge is obviously to implement Class#singleton_of without parsing the
output of inspect or, at least so that it works for all cases. the return
value of Class#singleton_of ought to be pretty obivous - it's the thing on the
rhs of
class << any_object
release the hounds!
-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================