How can this work?
def initialize params
@name = params[:name]
@collection << B.new
class B < A
@my_name = lookup_something(<parent.name>)
I don't really know what you are trying to do here - all the code you posted works on instance variables. Instance variables are per-object, not per class, so I don't know what you mean when you say "I need a value from the parent class".
There are several things that I note with your code:
A#initializeis not a good thing, because the base uses the subclass.
B#initializewith a completely different behaviour than
@nameis never assigned, because the
A#initializemethod is never called. You will have to either call
super(:name => name)(to run the superclass
initialize) or you have to assign to
@nameis defined, all your instance methods can use its value. Including the ones that are defined in the parent/superclass.
Let me say I'm not quite sure why you are using inheritance here, I have the feeling that it is not what you want. If you have two classes for two different things, there should never be inheritance involved. If you want to re-use code, either have a look at Ruby's mixin feature, or compose your complex objects of several "smaller" objects that have the behaviour you want to re-use.