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!
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:
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.
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
------------------------------------------------------------ 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
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.
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.
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).
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:
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.
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
···
On Fri, 27 Jun 2003 13:21:42 +0200, Robert Klemme wrote:
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
No, I’m sorry, but I think in this case you’re just plain wrong. 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:
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
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. 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:
OK… I wasn’t aware that Ruby does the job for you (for free)
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. 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.
Many of my postings cause more confusion than understanding
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:
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! )
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. I speak only English and
a few words of German.
True…
Many of my postings cause more confusion than understanding
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
#! /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
Wir könnten ja eine Sprache nehmen, die wir alle gleich schlecht
beherrschen. Vielleicht Latein oder Esperanto? :-))
Newspeak as in ‘vim mishighlights statement’
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
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! )
Many of my postings cause more confusion than understanding
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
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! )
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: