Trouble with inherited

Anyone have an idea of why the following code doesn’t work?

class A
class << self
def inherited(subclass)
name = subclass.my_name
puts "the inheriting classes name is ‘#{name}’"
end
end
end

class B < A
class << self
def my_name
"the B class"
end
end
end

It fails with:

t.rb:4:in inherited': undefined methodmy_name’ for B:Class
(NameError)
from t.rb:10

Steve Tuckner

Anyone have an idea of why the following code doesn't work?

::inherited is called before the content of the class is evaluated (i.e.
before B::my_name is defined)

Guy Decoux

What suggestion do you have to get information from the subclass that is
inheriting from the superclass?

Steve Tuckenr

···

-----Original Message-----
From: ts [mailto:decoux@moulon.inra.fr]
Sent: Friday, July 25, 2003 11:18 AM
To: ruby-talk ML
Cc: ruby-talk@ruby-lang.org
Subject: Re: Trouble with inherited

Anyone have an idea of why the following code doesn’t work?

::inherited is called before the content of the class is
evaluated (i.e.
before B::my_name is defined)

Guy Decoux

What suggestion do you have to get information from the subclass that is
inheriting from the superclass?

What information do you want ? In your case, the class was just created
and has not yet specifics methods.

Guy Decoux

In my case, I am interested in getting a string that would be specific to
that class (and not the class name). I am using this name as a hash key from
the name to the class.

Now I can simply save the class in a list and then when the hash would be
used later, I can simply generate the hash at that time for each request.
This seems not as elegant but probably is not much of a real speed penalty.

Is there a better more elegant way of doing this?

Steve

···

-----Original Message-----
From: ts [mailto:decoux@moulon.inra.fr]
Sent: Friday, July 25, 2003 11:40 AM
To: ruby-talk ML
Cc: ruby-talk@ruby-lang.org
Subject: Re: Trouble with inherited

What suggestion do you have to get information from the
subclass that is
inheriting from the superclass?

What information do you want ? In your case, the class was
just created
and has not yet specifics methods.

Guy Decoux

In my case, I am interested in getting a string that would be specific to
that class (and not the class name). I am using this name as a hash key
from
the name to the class.

Now I can simply save the class in a list and then when the hash would be
used later, I can simply generate the hash at that time for each request.
This seems not as elegant but probably is not much of a real speed
penalty.

Is there a better more elegant way of doing this?

Basically you have to delay things until the information in your
subclass becomes available. For example you could define a thread
in inherited which checks for this information at fixed time intervals
or something like this

···

“Steve Tuckner” STUCKNER@MULTITECH.COM wrote


class A
def self.singleton_method_added(sym)
super
unless self.equal?(A) or sym != :my_name
name = self.my_name
puts “the inheriting classes name is ‘#{name}’”
end
end
end

class B < A
class << self
def my_name
“the B class”
end
end
end

(This probably doesn’t work with 1.6 …)

/Christoph

In my case, I am interested in getting a string that would be specific to
that class (and not the class name). I am using this name as a hash key from
the name to the class.

Well, you can use the id of the class : it's specific to an object.

svg% ruby -e 'class A;class B;end;end;a = A::B.id;p a;p ObjectSpace._id2ref(a)'
537186582
svg%

Guy Decoux

···

A::B

“Steve Tuckner” STUCKNER@MULTITECH.COM schrieb im Newsbeitrag
news:0c3a01c352d9$16fc9a20$6904a8c0@multitech.prv…

In my case, I am interested in getting a string that would be specific
to
that class (and not the class name). I am using this name as a hash key
from
the name to the class.

Now I can simply save the class in a list and then when the hash would
be
used later, I can simply generate the hash at that time for each
request.
This seems not as elegant but probably is not much of a real speed
penalty.

Is there a better more elegant way of doing this?

Why don’t you just do this?

class A
@@subhash = {}

def self.get(key)
@@subhash[key]
end
end

class B < A
@@subhash[“my key”]=self
end

A.get(“my key”)

Subclasses have to obey a certain convention anyway (i.e. providing the
key) so you might as well do it this way.

Otherwise you could do

class A
class << self
def inherited(subclass)
(@subqueue||=) << subclass
end

def lookup(key)
  while ( @subqueue && cl = @subqueue.shift )
    (@subhash||={})[ cl.my_name ] = cl
  end

  @subhash && @subhash[key]
end

end
end

class B < A
class << self
def my_name
“the B class”
end
end
end

A.lookup “the B class”

But I’d regard this a bit complicated.

robert