E
Edgardo Hames
Hi you all.
I'm writing a collection class, which looks like this (by heart, don't
have the code here)
class Collection
def initialize(elements=nil)
@elements=elements
end
def add(elements)
elements.each{ |o| @elements << o unless @elements.include? o}
end
def sortable_by=(element_attribute)
@sortable_by=element_attribute
end
end
I need some help with the following issues:
0) I would like to be able to pass either an array or an single object
to the constructor, but (a) @elements should be a list of objects (not
an array containing arrays). I tried this
def initialize(o, *array)
@elements << o
array.each{ |o| @elements << o unless @elements.include? o}
end
but (a) is not satisfied when I do
c = Collection.new([1,2,3,4])
The same goes for the #add method.
1) I would like to use the #sortable_by method to indicate which
attribute of the collection elements should be compared when sorting
it. Right now, I'm sorting it like this
def sort(&block)
if block_given?
@elements.sort(&block)
else
@elements.sort{|x,y| x.send(@sortable_by) <=> y.send(@sortable_by)}
end
end
but I would like to implement the #<=> method in my objects, so I can
delegate the #sort method to @elements. I came up with this,
def sortable_by=(element_attribute)
@sortable_by=element_attribute
@elements.each{|o| o.sortable_by=@sortable_by}
end
and I update every new object I add. If I had a Java Comparator like
class, that would be trivial.
Can anybody point out some suggestions on how to do this in a better way.
Thanks for reading such a long message.
Kind regards,
Ed
I'm writing a collection class, which looks like this (by heart, don't
have the code here)
class Collection
def initialize(elements=nil)
@elements=elements
end
def add(elements)
elements.each{ |o| @elements << o unless @elements.include? o}
end
def sortable_by=(element_attribute)
@sortable_by=element_attribute
end
end
I need some help with the following issues:
0) I would like to be able to pass either an array or an single object
to the constructor, but (a) @elements should be a list of objects (not
an array containing arrays). I tried this
def initialize(o, *array)
@elements << o
array.each{ |o| @elements << o unless @elements.include? o}
end
but (a) is not satisfied when I do
c = Collection.new([1,2,3,4])
The same goes for the #add method.
1) I would like to use the #sortable_by method to indicate which
attribute of the collection elements should be compared when sorting
it. Right now, I'm sorting it like this
def sort(&block)
if block_given?
@elements.sort(&block)
else
@elements.sort{|x,y| x.send(@sortable_by) <=> y.send(@sortable_by)}
end
end
but I would like to implement the #<=> method in my objects, so I can
delegate the #sort method to @elements. I came up with this,
def sortable_by=(element_attribute)
@sortable_by=element_attribute
@elements.each{|o| o.sortable_by=@sortable_by}
end
and I update every new object I add. If I had a Java Comparator like
class, that would be trivial.
Can anybody point out some suggestions on how to do this in a better way.
Thanks for reading such a long message.
Kind regards,
Ed