[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

getting an array of unique objects of a class i wrote

Adam Akhtar

9/14/2008 11:08:00 AM

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 via http://www.ruby-....

5 Answers

Lex Williams

9/14/2008 11:15:00 AM

0

use a hash
--
Posted via http://www.ruby-....

Michael Guterl

9/14/2008 11:22:00 AM

0

On Sun, Sep 14, 2008 at 7:08 AM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> 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?

You need to implement Thing#eql? and Thing#hash in order for a
Array#uniq, Set, or Hash to function as you expect.

Sebastian Hungerecker

9/14/2008 11:30:00 AM

0

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
--
NP: Die Apokalyptischen Reiter - Heut' Ist Der Tag
Jabber: sepp2k@jabber.org
ICQ: 205544826

Adam Akhtar

9/16/2008 10:00:00 AM

0

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 via http://www.ruby-....

Brian Candler

9/16/2008 11:49:00 AM

0

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.
--
Posted via http://www.ruby-....