From: Daniel N Date: 2006-10-09T21:02:07+09:00 Subject: Re: Removing Duplicate Objects from Object List ------=_Part_147795_7630551.1160395323712 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/9/06, Jeff Nyman wrote: > > "gaspode" wrote in message > news:1160393869.844030.8080@m7g2000cwm.googlegroups.com... > > > How are you storing the Rules in your RuleSet at the moment? Personally > > I'd use an Array (or simply subclass Array) and then you get to use > > Array.uniq without shifting objects back and forth. > > Essentially, I have a RuleList class like this: > > > class RuleList > def initialize > @rules = Array.new > end > > def append(this_rule) > @rules.push(this_rule) > end > > def selection > @rules.find_all { |rule| rule } > end > end > > > Then I have a Rule class like this: > > > class Rule > attr_accessor :point, :filter, :value > > def initialize(point, filter, value) > @point = point > @filter = filter > @value = value > end > > def to_s > "#@point, #@filter, #@value" > end > end > > > When a rule object needs to be added to the list, I do this: > > $ruleList.append(Rule.new(step.point2, rule, value)) > > Does that give enough detail? > > In playing around a bit more, I tried this: > > rules_array = $ruleList.selection.collect { |rule| rule } > > Then I tried: > > rules_array.uniq! > > The problem is that this finds nothing as a duplicate. But that makes > sense > (I think) because the object ID is probably being considered as part of > the > test and those will, of course, not be duplicates. > > It sounds like you're saying it would be better to not use a Rule class in > the first place. Is that accurate? > > - Jeff If instead of declaring your @rules as an array, you declare it as a set you will get no duplicates for free (I think) However, you need to incorporate the <=> operator in your Rule class to tell ruby how your objects relate to each other. ie are they <, >, or = ------=_Part_147795_7630551.1160395323712--