what is the definition of equal? in the case of array?
why is there another function eql? performing the actual equal test
Where exactly is it listed? It’s not listed in the docs for Array.
equal? is defined by Object and it is not overridden for any core class.
The same as for anything else: a.equal? b is true if and only if a
and b
are actually the same object (i.e. both a and b point to the same
location in
memory)
eql? does not perform the “actual equal test” - whatever that is.
eql? is
to be used together with hash (and is indeed used that way by Hash, Set
and
Array#&, Array#| and Array#-).
what is the definition of equal? in the case of array?
why is there another function eql? performing the actual equal test
My questions:
What conclusions did you draw from your test results?
What did the documentation say?
robert
Since I went through the documentation for the Array. equal? method
does not exist. eql? does exist. Prior to this – I used [1,1].methods
and it listed the methods that are available for the object. Now my
conclusion is that no to trust methods function – because it does not
do what it is supposed to do – that is allow a programmer to know what
methods are supported.
what is the definition of equal? in the case of array?
why is there another function eql? performing the actual equal test
Where exactly is it listed? It’s not listed in the docs for Array.
equal? is defined by Object and it is not overridden for any core class.
The same as for anything else: a.equal? b is true if and only if a
and b
are actually the same object (i.e. both a and b point to the same
location in
memory)
you are correct to say that equal? is not in the Array docs. However,
when I executed
a = [1,1]
a.methods
equal? method is there on the list of methods supported by the object
Array that is used by the variable a.
eql? does not perform the “actual equal test” - whatever that is.
eql? is
to be used together with hash (and is indeed used that way by Hash, Set
and
"array.eql?(other) → true or false
Returns true if array and other are the same object, or are both arrays
with the same content. " — from http://ruby-doc.org/core/classes/Array.html
Array#&, Array#| and Array#-).
4. The method that I assume you’re looking for is ==
what is the definition of equal? in the case of array?
why is there another function eql? performing the actual equal test
Where exactly is it listed? It’s not listed in the docs for Array.
equal? is defined by Object and it is not overridden for any core class.
The same as for anything else: a.equal? b is true if and only if a
and b
are actually the same object (i.e. both a and b point to the same
location in
memory)
you are correct to say that equal? is not in the Array docs. However,
when I executed
a = [1,1]
a.methods
equal? method is there on the list of methods supported by the object
Array that is used by the variable a.
eql? does not perform the “actual equal test” - whatever that is.
eql? is
to be used together with hash (and is indeed used that way by Hash, Set
and
"array.eql?(other) → true or false
Returns true if array and other are the same object, or are both arrays
with the same content. " — from class Array - RDoc Documentation
Array#&, Array#| and Array#-).
4. The method that I assume you’re looking for is ==
HTH,
Sebastian
I did find the reference page that you probably used to explain. I got
the reference that explains my imprise definition of equality that you
were trying to explain.
Now my
conclusion is that no to trust methods function – because it does not
do what it is supposed to do – that is allow a programmer to know what
methods are supported.
Wtf? [].methods lists equal? because equal? is defined by Object and is
as
such available to be called on all objects including arrays. It is not
listed
in the docs of Array because it is defined by Object, not Array, and is
not
redefined by Array either. You can perfectly well call equal? on an
array and
it will behave like the docs for Object#equal? say it will.
Now my
conclusion is that no to trust methods function – because it does not
do what it is supposed to do – that is allow a programmer to know what
methods are supported.
Wtf? [].methods lists equal? because equal? is defined by Object and is
as
such available to be called on all objects including arrays. It is not
listed
in the docs of Array because it is defined by Object, not Array, and is
not
redefined by Array either. You can perfectly well call equal? on an
array and
it will behave like the docs for Object#equal? say it will.
HTH,
Sebastian
You gotta chill, I am trying to use a language – not get anyone riled
up…
smile and be happy
The equal? method comes from the Object class. The equal? method will
return true if and only if the two objects are the same object. The two
lists are two independently created lists, so this will be false. The
eql? method is an alias for this method.
If you want to see if two arrays contain the same objects, use the ==
operator.
Not on Array, it’s not. Array#eql? behaves like Array#==, not like
Object#equal?.
The statement isn’t true even in the general case: eql? and ==
implement the mathematical concept of “equivalence” and have usually
the same semantics (there are some noteworthy differences for
numbers). eql? being the more important method as it is also used by
Hash and others to find equivalent entires. equal? on the contrary
implements the concept of “identity”. The default behavior of this in
class Object should never be overridden - but then again, the method
is used so sparingly that it probably won’t matter.
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.