Forum: Ruby getting an array of unique objects of a class i wrote

Posted by Adam Akhtar (thegeezer3)
on 2008-09-14 13:15
Hi Ive done some searching on this but couldnt find an answer that was
either totally appropriate or one that i could understand ;> (some
advanced stuff going on).

So if i have a class with two variables (strings) like this

class thing
attr_accessor :name :job
end

and i want to ensure that if i have an array of these objects there will
be no duplicates. A duplicate would be where both the name and job are
the same in both objects.

I tried using uniq! but it didnt work. Not sure why (can someone explain
that).

Ive heard of set, could that be used in this case to prevent duplicates
being added?
Posted by geo ssscripting (vhaerun)
on 2008-09-14 13:21
use a hash
Posted by Michael Guterl (Guest)
on 2008-09-14 13:31
(Received via mailing list)
On Sun, Sep 14, 2008 at 7:08 AM, Adam Akhtar <adamtemporary@gmail.com> 
wrote:
> and i want to ensure that if i have an array of these objects there will
> be no duplicates. A duplicate would be where both the name and job are
> the same in both objects.
>
> I tried using uniq! but it didnt work. Not sure why (can someone explain
> that).
>
> Ive heard of set, could that be used in this case to prevent duplicates
> being added?

You need to implement Thing#eql? and Thing#hash in order for a
Array#uniq, Set, or Hash to function as you expect.
Posted by Sebastian Hungerecker (Guest)
on 2008-09-14 13:37
(Received via mailing list)
Adam Akhtar wrote:
> class thing
> attr_accessor :name :job
> end

Thing = Struct.new(:name, :job)


> and i want to ensure that if i have an array of these objects there will
> be no duplicates. A duplicate would be where both the name and job are
> the same in both objects.
>
> I tried using uniq! but it didnt work. Not sure why (can someone explain
> that).

For uniq to work your class has to implement hash and eql?, so that for 
any
two objects that you consider equal o1.hash == o2.hash and o1.eql?(o2) 
and
for any two objects that you do not consider equal !o1.eql?(o2) (o1.hash 
may
still be equal to o2.hash, but should not be).


> Ive heard of set, could that be used in this case to prevent duplicates
> being added?

If uniq does not work, set does not work either. Once you implement hash 
and
eql?, yes, set can be used.
If you use Struct to define your class, like I demonstrated above hash 
and
eql? will be defined automatically. If you can't use a struct, you could
define them as such:
class Thing
  attr_accessor :name, :job
  def hash
    [name, job].hash
  end

  def eql?(other)
    [name, job].eql?([other.name, other.job])
  end
end


HTH,
Sebastian
Posted by Adam Akhtar (thegeezer3)
on 2008-09-16 12:06
Thank you everyone for your reply. Ive implemented the hash and eql? 
definitions for my class and it worked. On a similiar note what do i 
have to do for my unit tests which compare two objects to work.

I.e.

assert_equal(expected_object, some_method_which_returns_an_object())

where both the compared objects are instances of a class i defined.

So far my unit tests fail despite the variables within the objects being 
identical. the only difference is the object_id which i guess is 
important.
I impleted hash and eql? like above but the tests still dont pass.

Posted by Brian Candler (candlerb)
on 2008-09-16 13:56
Adam Akhtar wrote:
> Thank you everyone for your reply. Ive implemented the hash and eql? 
> definitions for my class and it worked. On a similiar note what do i 
> have to do for my unit tests which compare two objects to work.
> 
> I.e.
> 
> assert_equal(expected_object, some_method_which_returns_an_object())

Look at the source for assert_equal :-)
/usr/lib/ruby/1.8/test/unit/assertions.rb

      def assert_equal(expected, actual, message=nil)
...
        assert_block(full_message) { expected == actual }

So your object needs to implement '==' too.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.