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.
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
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
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.
“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