Using & for arrays of objects

Hi,
I’m having trouble using the & (intersection) method on two arrays of
objects. I know that if the objects don’t have comparison methods, & won’t
work because it is looking at the object IDs rather than values stored in
the objects, but I can’t seem to get & to use the comparison methods I want.

Thanks,
Krishna

A silly class that stores a single number.

class Trivial
def initialize(n)
@n = n
end
def n
@n
end

# I hoped these two methods would allow & to work on arrays containing this

class.
def ==(anOther)
@n == anOther.n
end
def <=>(anOther)
@n <=> anOther.n
end
end

a = Trivial.new(6)
b = Trivial.new(6)

p (a == b) # this returns true, like I hoped
p (a<=>b) # this returns 0, also good

Now make some arrays

foo = [Trivial.new(1), Trivial.new(12)]
bar = [Trivial.new(1), Trivial.new(4)]

p (foo[0] == bar[0]) # returns true
p (foo[0] <=> bar[0]) # returns 0 – Great! everything seems to be working!

p (foo & bar) # returns an empty array! NOOOOO!!!

Hi,
I’m having trouble using the & (intersection) method on two arrays of
objects. I know that if the objects don’t have comparison methods, & won’t
work because it is looking at the object IDs rather than values stored in
the objects, but I can’t seem to get & to use the comparison methods I want.

[snip code]

p (foo & bar) # returns an empty array! NOOOOO!!!

There is no intersection(&) method.

I guess you have to do

def &(anOther)
@n & anOther.n
end

Have you looked at ‘set.rb’ which is distributed with ruby ?

···

On Fri, 27 Jun 2003 10:48:56 +0900, Krishna Dole wrote:


Simon Strandgaard

Hi,

···

At Fri, 27 Jun 2003 09:48:56 +0900, Krishna Dole wrote:

I’m having trouble using the & (intersection) method on two arrays of
objects. I know that if the objects don’t have comparison methods, & won’t
work because it is looking at the object IDs rather than values stored in
the objects, but I can’t seem to get & to use the comparison methods I want.

Array#& uses Hash internally due to performance, so you need to
define #hash and #eql? methods.


Nobu Nakada

Hi,
I’m having trouble using the & (intersection) method on two arrays of
objects. I know that if the objects don’t have comparison methods, & won’t
work because it is looking at the object IDs rather than values stored in
the objects, but I can’t seem to get & to use the comparison methods I want.

& uses hashes under the hood. You need to define #hash and #eql? for your
object so that any object you want to be considered equal have equal hash
values and eql?(other) returns true. The easiest way would be like this:

class N
def initialize(n)
@n = n
end
def hash()
@n.hash()
end
def eql?(other)
@n.hash() == other.hash()
end
attr_reader :n
end

In use:

[ N.new(1), N.new(2) ] & [ N.new(2), N.new(3) ]
=> [#<N:0x402a83a4 @n=2>]

~/prog/ruby$ ri Object#eql?
This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

------------------------------------------------------------ Object#eql?
obj.eql?( anObject ) → true or false

···

On Fri, 27 Jun 2003 09:48:56 +0900 “Krishna Dole” kpdole@ucdavis.edu wrote:

 Returns true if obj and anObject have the same value. Used by Hash
 to test members for equality. For objects of class Object, eql? is
 synonymous with ==. Subclasses normally continue this tradition,
 but there are exceptions. Numeric types, for example, perform type
 conversion across ==, but not across eql?, so:
    1 == 1.0     #=> true
    1.eql? 1.0   #=> false

~/prog/ruby$ ri Object#hash
This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

------------------------------------------------------------ Object#hash
obj.hash → aFixnum

 Generates a Fixnum hash value for this object. This function must
 have the property that a.eql?(b) implies a.hash == b.hash. The hash
 value is used by class Hash. Any hash value that exceeds the
 capacity of a Fixnum will be truncated before being used.

Jason Creighton

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.27.05.49.55.670426@sneakemail.com

Hi,
I’m having trouble using the & (intersection) method on two arrays of
objects. I know that if the objects don’t have comparison methods, &
won’t
work because it is looking at the object IDs rather than values stored
in
the objects, but I can’t seem to get & to use the comparison methods I
want.

[snip code]

p (foo & bar) # returns an empty array! NOOOOO!!!

There is no intersection(&) method.

There is:

07:31:22 [temp]: ri ‘Array#&’
---------------------------------------------------------------- Array#&
arr & anOtherArray → anArray

···

On Fri, 27 Jun 2003 10:48:56 +0900, Krishna Dole wrote:


 Set Intersection---Returns a new array containing elements common
 to the two arrays, with no duplicates.
    [ 1, 1, 3, 5 ] & [ 1, 2, 3 ]   #=> [1, 3]

07:31:25 [temp]:

Have you looked at ‘set.rb’ which is distributed with ruby ?

This should work with Arrays, too. I’ve played around with class Trivial,
even overloaded eql?(), ==(), equal?(), id(), id() and object_id() to
to no avail. The array from the union remains empty. IMHO this is a bug
(my version is 1.7.3).

Regards

robert

I barely dare asking this because I know how tedious of a task
maintaning documentation is. Nevertheless, could it be made clearer
throughout the documentation what equality operator (hash/eql? or ==) a
method relies on. It is not the first time I see confusion about it and
I am a little confused myself from time to time.

Just looking at the Array class, at least the following methods rely on
an equality operator of its elements:

-, &, delete (with no block), uniq, uniq!, eql?, <=>, ==, include?,
index, rassoc, rindex.

The methods from eql? on already specify which equality operator is
being used.

Thanks in advance,
Guillaume.

···

On Fri, 2003-06-27 at 07:26, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 27 Jun 2003 09:48:56 +0900, > Krishna Dole wrote:

I’m having trouble using the & (intersection) method on two arrays of
objects. I know that if the objects don’t have comparison methods, & won’t
work because it is looking at the object IDs rather than values stored in
the objects, but I can’t seem to get & to use the comparison methods I want.

Array#& uses Hash internally due to performance, so you need to
define #hash and #eql? methods.


Nobu Nakada

I was refering to the code posted in the root of this thread.
You can see that the adjustment I proposed uses Array#&.

:slight_smile:

···

On Fri, 27 Jun 2003 10:41:55 +0200, Robert Klemme wrote:

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im

There is no intersection(&) method.

There is:
07:31:22 [temp]: ri ‘Array#&’


Simon Strandgaard

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.27.08.07.16.538696@sneakemail.com

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im

There is no intersection(&) method.

There is:
07:31:22 [temp]: ri ‘Array#&’

I was refering to the code posted in the root of this thread.
You can see that the adjustment I proposed uses Array#&.

Sorry, I don’t seem to understand you. The OP used Array#& and it is
defined. What did you want to say with “There is no intersection(&)
method”?

Extract from thread root

Now make some arrays

foo = [Trivial.new(1), Trivial.new(12)]
bar = [Trivial.new(1), Trivial.new(4)]

p (foo[0] == bar[0]) # returns true
p (foo[0] <=> bar[0]) # returns 0 – Great! everything seems to be
working!

p (foo & bar) # returns an empty array! NOOOOO!!!

Regards

robert
···

On Fri, 27 Jun 2003 10:41:55 +0200, Robert Klemme wrote:

OK… What I meant with “no intersection method” was: That his class did
not have a Trival#& method defined. I suggested him to use Array#& to
do the intersection for him.

Sorry, I am terrible at communition :slight_smile:

···

On Fri, 27 Jun 2003 13:21:42 +0200, Robert Klemme wrote:

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.27.08.07.16.538696@sneakemail.com

On Fri, 27 Jun 2003 10:41:55 +0200, Robert Klemme wrote:

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im

There is no intersection(&) method.

There is:
07:31:22 [temp]: ri ‘Array#&’

I was refering to the code posted in the root of this thread.
You can see that the adjustment I proposed uses Array#&.

Sorry, I don’t seem to understand you. The OP used Array#& and it is
defined. What did you want to say with “There is no intersection(&)
method”?


Simon Strandgaard

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.27.10.33.05.161749@sneakemail.com

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.27.08.07.16.538696@sneakemail.com

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im

There is no intersection(&) method.

There is:
07:31:22 [temp]: ri ‘Array#&’

I was refering to the code posted in the root of this thread.
You can see that the adjustment I proposed uses Array#&.

Sorry, I don’t seem to understand you. The OP used Array#& and it is
defined. What did you want to say with “There is no intersection(&)
method”?

OK… What I meant with “no intersection method” was: That his class did
not have a Trival#& method defined. I suggested him to use Array#& to
do the intersection for him.

Sorry, I am terrible at communition :slight_smile:

No, I’m sorry, but I think in this case you’re just plain wrong. :slight_smile: I
don’t think the class needs a method & because array intersection
(Array#&) is defined as returning a single array that contains only
elements in self and the argument array. Thus elements need only be
comparable or similar. No Trivial#& required. As Nobu Nakada pointed out
it was the hash method that was missing (at least in during my
experimenting).

Kind regards

robert
···

On Fri, 27 Jun 2003 13:21:42 +0200, Robert Klemme wrote:

On Fri, 27 Jun 2003 10:41:55 +0200, Robert Klemme wrote:

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.27.08.07.16.538696@sneakemail.com

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im

There is no intersection(&) method.

There is:
07:31:22 [temp]: ri ‘Array#&’

I was refering to the code posted in the root of this thread.
You can see that the adjustment I proposed uses Array#&.

Sorry, I don’t seem to understand you. The OP used Array#& and it is
defined. What did you want to say with “There is no intersection(&)
method”?

OK… What I meant with “no intersection method” was: That his class did
not have a Trival#& method defined. I suggested him to use Array#& to
do the intersection for him.

Sorry, I am terrible at communition :slight_smile:

Apparently the only problem is that you are both
conversing in your “second” language, English.

Most of the misunderstandings on this list are
the result of the use of English by non-native
speakers.

I cannot criticize. :slight_smile: I speak only English and
a few words of German.

Hal

···

----- Original Message -----
From: “Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, June 27, 2003 5:45 AM
Subject: Re: Using & for arrays of objects

On Fri, 27 Jun 2003 13:21:42 +0200, Robert Klemme wrote:

On Fri, 27 Jun 2003 10:41:55 +0200, Robert Klemme wrote:


Hal Fulton
hal9000@hypermetrics.com

OK… I wasn’t aware that Ruby does the job for you (for free) :slight_smile:

I was thinking: The intersection between two Trivial instances should not
return an array… instead it should return an instance of Trivial
containing the result array… like this:

def &(anOther)
Trivial.new(@n & anOther.n)
end

···

On Fri, 27 Jun 2003 18:07:39 +0200, Robert Klemme wrote:

No, I’m sorry, but I think in this case you’re just plain wrong. :slight_smile: I
don’t think the class needs a method & because array intersection
(Array#&) is defined as returning a single array that contains only
elements in self and the argument array. Thus elements need only be
comparable or similar.


Simon Strandgaard

True…

Many of my postings cause more confusion than understanding :slight_smile:

I did especialy a terrible job, when I suggested the RCR
if Array#join should take a block. If I had better english
skills then I would redo this RCR. I feel guilty!

···

On Sat, 28 Jun 2003 03:38:49 +0900, Hal E. Fulton wrote:

From: “Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com

Sorry, I am terrible at communition :slight_smile:

Apparently the only problem is that you are both
conversing in your “second” language, English.

Most of the misunderstandings on this list are
the result of the use of English by non-native
speakers.

I cannot criticize. :slight_smile: I speak only English and
a few words of German.


Simon Strandgaard
I wish a upgrade of my english skills for christmas.

“Hal E. Fulton” hal9000@hypermetrics.com schrieb im Newsbeitrag
news:017301c33cd3$eb075c40$0300a8c0@austin.rr.com

Apparently the only problem is that you are both
conversing in your “second” language, English.

Most of the misunderstandings on this list are
the result of the use of English by non-native
speakers.

I cannot criticize. :slight_smile: I speak only English and
a few words of German.

Wir könnten ja eine Sprache nehmen, die wir alle gleich schlecht
beherrschen. Vielleicht Latein oder Esperanto? :-))

Hihi…

robert

Hey, don’t feel bad. I think you did a good job of explaining why you want
#join to accept a block. (I still think it’s a bad idea, for reasons given in
the comments of that RCR.)

With English being my first (Actually, my only) language, I still managed to
sumbit my RCR (http://rubygarden.org/article.php?sid=305) without realizing
that you could do what I wanted with RUBYOPT. How could I possibly be so
dense? (Don’t answer that! :slight_smile: )

Jason Creighton

···

On Fri, 27 Jun 2003 20:04:13 +0200 “Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com wrote:

On Sat, 28 Jun 2003 03:38:49 +0900, Hal E. Fulton wrote:

From: “Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com

Sorry, I am terrible at communition :slight_smile:

Apparently the only problem is that you are both
conversing in your “second” language, English.

Most of the misunderstandings on this list are
the result of the use of English by non-native
speakers.

I cannot criticize. :slight_smile: I speak only English and
a few words of German.

True…

Many of my postings cause more confusion than understanding :slight_smile:

I did especialy a terrible job, when I suggested the RCR
if Array#join should take a block. If I had better english
skills then I would redo this RCR. I feel guilty!
http://www.rubygarden.org/article.php?sid=301

Scripsit ille »Robert Klemme« bob.news@gmx.net:

“Hal E. Fulton” hal9000@hypermetrics.com schrieb im Newsbeitrag news:017301c33cd3$eb075c40$0300a8c0@austin.rr.com

Apparently the only problem is that you are both
conversing in your “second” language, English.

Most of the misunderstandings on this list are
the result of the use of English by non-native
speakers.

I cannot criticize. :slight_smile: I speak only English and
a few words of German.

Wir könnten ja eine Sprache nehmen, die wir alle gleich schlecht
beherrschen. Vielleicht Latein oder Esperanto? :-))

Not too loud, otherwise Matz might start pretending he isn’t good at
Japanese.

I think we should extend Ruby by the why, why_not, wtf?, i_think
keywords and the must, should, may, may_not, should_not and must_not
constructs.

i_think {
if we.do.to_past() |that|
should_not {
problems.with(that).any().exists?()
}
end
}

···


For the sake of who I am, I am myself. [EoE Part II, translated]

You can speak latin with PERL:

http://www.csse.monash.edu.au/~damian/papers/HTML/Perligata.html

Ex: the Sieve of Eratosthenes

    #! /usr/local/bin/perl -w

    use Lingua::Romana::Perligata;

    maximum inquementum tum biguttam egresso scribe.
    meo maximo vestibulo perlegamentum da.
    da duo tum maximum conscribementa meis listis.

    dum listis decapitamentum damentum nexto
        fac sic
            nextum tum novumversum scribe egresso.
            lista sic hoc recidementum nextum cis vannementa da

listis.
cis.

Isn’t grand?

Guillaume.

···

On Mon, 2003-06-30 at 05:35, Robert Klemme wrote:

“Hal E. Fulton” hal9000@hypermetrics.com schrieb im Newsbeitrag
news:017301c33cd3$eb075c40$0300a8c0@austin.rr.com

Apparently the only problem is that you are both
conversing in your “second” language, English.

Most of the misunderstandings on this list are
the result of the use of English by non-native
speakers.

I cannot criticize. :slight_smile: I speak only English and
a few words of German.

Wir könnten ja eine Sprache nehmen, die wir alle gleich schlecht
beherrschen. Vielleicht Latein oder Esperanto? :-))

Hihi…

robert

Saluton!

  • Robert Klemme; 2003-06-30, 12:45 UTC:

Wir könnten ja eine Sprache nehmen, die wir alle gleich schlecht
beherrschen. Vielleicht Latein oder Esperanto? :-))

Newspeak as in ‘vim mishighlights statement’ :slight_smile:

Gis,

Josef ‘Jupp’ Schugt

···


Someone even submitted a fingerprint for Debian Linux running on the
Microsoft Xbox. You have to love that irony :).
– Fyodor on nmap-hackers@insecure.org

No silver bullets.

No language can compensate lack of caffeine :slight_smile:

Ruby is close to it, but still not there :wink:

···

On Sat, Jun 28, 2003 at 05:33:10AM +0900, Jason Creighton wrote:

With English being my first (Actually, my only) language, I still managed to
sumbit my RCR (http://rubygarden.org/article.php?sid=305) without realizing
that you could do what I wanted with RUBYOPT. How could I possibly be so
dense? (Don’t answer that! :slight_smile: )


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

After 14 non-maintainer releases, I’m the S-Lang non-maintainer.
– Ray Dassen

Many of my postings cause more confusion than understanding :slight_smile:

I did especialy a terrible job, when I suggested the RCR
if Array#join should take a block. If I had better english
skills then I would redo this RCR. I feel guilty!
http://www.rubygarden.org/article.php?sid=301

Hey, don’t feel bad. I think you did a good job of explaining why you want
#join to accept a block. (I still think it’s a bad idea, for reasons given in
the comments of that RCR.)

I only feel bad about my english skills. Otherwise I feel good :slight_smile:
I think Ruby+‘test-first’ has a made me a much more happy person.

With English being my first (Actually, my only) language, I still managed to
sumbit my RCR (http://rubygarden.org/article.php?sid=305) without realizing
that you could do what I wanted with RUBYOPT. How could I possibly be so
dense? (Don’t answer that! :slight_smile: )

Agree, It happens for me all the time… waking up next morning just to
realize that I did a lame posting just before I went to sleep.

···

On Fri, 27 Jun 2003 15:20:49 +0000, Jason Creighton wrote:

On Fri, 27 Jun 2003 20:04:13 +0200 > “Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com wrote:


Simon Strandgaard