S
Stephan Kämper
Hi all,
suppose I have some kind of object which holds 'object ids' referring to
other objects, probably objects of its own class - or even itself.
Now adding an object to the list can be done (at least) in two ways:
Pass an object to the method and let the method find out the object id,
or pass the object id directly.
Now the following lines are just an example of what I'm thinking about
(and it's certainly not thread safe among other things).
class Common_Base
@@curr_oid = "aa"
attr_reader id
def initialize
@oid = Common_Base.get_new_oid
end
def Common_Base.get_new_oid
@@curr_oid = @@curr_oid.succ
end
end
class Reference_List < Common_Base
def initialize
super
@refs = []
end
def add_ref1( other )
@refs << other.oid unless @refs.include? other.oid
end
def add_ref2( oid )
@refs << oid unless @refs.include? oid
end
end
class Referenced < Common_Base
end
refl = Reference_List.new
ref = Referenced.new
refl.add_ref1( ref )
p refl # -> <Reference_List:0x27862b0 @oid="ab", @refs=["ac"]>
refl.add_ref2( ref.oid )
p refl # -> <Reference_List:0x27862b0 @oid="ab", @refs=["ac"]>
refl.add_ref1( refl )
p refl # -> <Reference_List:0x27862b0 @oid="ab", @refs=["ac", "ab"]>
Now, for 'add_ref1' the method needs to know a bit more about the object
to be added (needs to call the oid method of the added method), while
'add_ref2' seems to put more work on the caller of the method (needs to
ask for the added oid).
Anyway, right now using 'add_ref1' _seems_ to be more the
'tell-the-other-guy' way, but I'm not too sure about that...
So, what's the better way to add a to-be-referenced object to the
referencing object?
Any input is welcome.
Happy rubying
Stephan
[1] See http://www.pragmaticprogrammer.com/articles/may_04_oo1.pdf for
more on that.
suppose I have some kind of object which holds 'object ids' referring to
other objects, probably objects of its own class - or even itself.
Now adding an object to the list can be done (at least) in two ways:
Pass an object to the method and let the method find out the object id,
or pass the object id directly.
Now the following lines are just an example of what I'm thinking about
(and it's certainly not thread safe among other things).
class Common_Base
@@curr_oid = "aa"
attr_reader id
def initialize
@oid = Common_Base.get_new_oid
end
def Common_Base.get_new_oid
@@curr_oid = @@curr_oid.succ
end
end
class Reference_List < Common_Base
def initialize
super
@refs = []
end
def add_ref1( other )
@refs << other.oid unless @refs.include? other.oid
end
def add_ref2( oid )
@refs << oid unless @refs.include? oid
end
end
class Referenced < Common_Base
end
refl = Reference_List.new
ref = Referenced.new
refl.add_ref1( ref )
p refl # -> <Reference_List:0x27862b0 @oid="ab", @refs=["ac"]>
refl.add_ref2( ref.oid )
p refl # -> <Reference_List:0x27862b0 @oid="ab", @refs=["ac"]>
refl.add_ref1( refl )
p refl # -> <Reference_List:0x27862b0 @oid="ab", @refs=["ac", "ab"]>
Now, for 'add_ref1' the method needs to know a bit more about the object
to be added (needs to call the oid method of the added method), while
'add_ref2' seems to put more work on the caller of the method (needs to
ask for the added oid).
Anyway, right now using 'add_ref1' _seems_ to be more the
'tell-the-other-guy' way, but I'm not too sure about that...
So, what's the better way to add a to-be-referenced object to the
referencing object?
Any input is welcome.
Happy rubying
Stephan
[1] See http://www.pragmaticprogrammer.com/articles/may_04_oo1.pdf for
more on that.