Project

General

Profile

This project is closed and read-only.

Actions

Misc #3401

closed

arrays containing them selves do not apply == sensibly.

Misc #3401: arrays containing them selves do not apply == sensibly.

Added by dmt10 (Dominic Tarr) over 16 years ago. Updated over 15 years ago.

Status:
Rejected
Assignee:
-
[ruby-core:30628]

Description

=begin
a = [:a]
a << a
puts a
=> [:a [...]]

b = [:a]
b << b
puts b
=> [:a [...]]

#so , a == b?
puts a == b
=> false

#but they have the same structure...
puts a.to_yaml == b.to_yaml
=> true

#it looks like == compares the object_id's of arrays within arrays.
#more evidence:

c = [:a]
c << a
puts c
=> [:a, [:a, [...]]]
#clearly, a distinctly different array.
puts a == c
=> true
puts a.to_yaml == c.to_yaml
=> false

#this seems surprising behaviour to me
=end

Updated by ujihisa (Tatsuhiro Ujihisa) over 16 years ago Actions #1

=begin
FYI ruby 1.9 changed this behavior

 $ ruby193 -ve 'a=[:a];a<<a;b=[:a];b<<b;p a;p b;p a==b'
 ruby 1.9.3dev (2010-06-06 trunk 28190) [i386-darwin9.8.0]
 [:a, [...]]
 [:a, [...]]
 true

=end

Updated by marcandre (Marc-Andre Lafortune) over 16 years ago Actions #2

  • Status changed from Open to Rejected

=begin
Ruby 1.9's treatment of recursive structures has been set so that two structures are equal if they contain the elements that are equal, or said otherwise if no difference can be found between them. If you can give a "path" x, y, z..., such that a[x][y][z] != b[x][y][z], then and only then will a != b.

In your example, c[0] == a[0], c[1] == a[1] and c.size == a.size, so c == a.
=end

Actions

Also available in: PDF Atom