Hi,
I have been trying to intersect 2 sets of objects that where I want the
“intersected” objects to share a common property.
This is what I came up with:
Here, I want to get all the Errors having the same file name:
def intersection_set
Set.new(event_errors).&(Set.new(socket_channel_errors)) {|x|
file_name(x)}
end
To allow that I extended the Set class with:
def &(enum, &property)
n = self.class.new
if block_given?
mapped = self.map{|e| property.call(e)}
enum.each { |o| n.add(o) if mapped.include?(property.call(o)) }
else
enum.each { |o| n.add(o) if include?(o) }
end
n
end
I have met this requirement quite some times while scripting and I may
miss something here.
Do you know a more idiomatic, Ruby-way of doing that?
Thanks,
Eric.
Eric TORREBORRE
tel: +81 (0)90 5580 3280
e-mail: [email protected] / [email protected]
blog: http://etorreborre.blogspot.com
_____________________________________________________________________________
Ne gardez plus qu’une seule adresse mail ! Copiez vos mails vers Yahoo!
Mail