>>>>> "C" == Christoph Rippel <crippel / primenet.com> writes:
>> From: ts [mailto:decoux / moulon.inra.fr]
>> >>>>> "M" == Mathieu Bouchard <matju / sympatico.ca> writes:

I'll try to explain what do ruby actually

M> def f(x); [x,x]; end
M> def g(x); f f f f f f f f x; end
M> (g g g 1) == (g g g 1) #=> true

In this case it do something like this :

class Array
def ==(other)
each_index do |i|
return false if not self[i] == other[i]
end
true
end
end

This explain why it has problem with recursive array.

>>
>> def f(x); [x,x]; end
>> def g(x); f f f f f f f f x; end
>> p (g g g 1)

In this case inspect is protected, and ruby do (this is *very* simplified)

class Array
def inspect
return '[]' if empty?
return '[...]' if @inspect.include? id
begin
@inspect.push id
str = '['
each do |i|
str << i.inspect
end
str << ']'
ensure
@inspect.pop
end
str
end
end

where @inspect is in reality a thread local variable.

This is why it take a very long time to find 'p (g g g 1)' because all
recursive calls are replaced with a 'begin ... ensure'

C> ##########################
C> p ([1,3,3,[3,4],"a"] == [1,3,3,[3,4],"a"])
C> p ([1,3,3,[3,4],"a"] == [1,3,3,[3,4],"b"])

C> ##########################
C> a =[1,2,3]; a << a; b =[1,2]; b << b;  a  << a; b << a;
C> p a - b; p a; p b

In this case it give the good result

pigeon% cat b.rb
#!./ruby
p ([1,3,3,[3,4],"a"] == [1,3,3,[3,4],"a"])
p ([1,3,3,[3,4],"a"] == [1,3,3,[3,4],"b"])

a =[1,2,3]; a << a; b =[1,2]; b << b;  a  << a; b << a;
p a - b; p a; p b
pigeon%

pigeon% b.rb
true
false

[1, 2, 3, [...], [...]]
[1, 2, [...], [1, 2, 3, [...], [...]]]
pigeon%

C> ##########################
C> def f(x); [x,x]; end
C> def g(x); f f f f f f f f x; end
C> def h(x); g g g g g g g g x; end
C> def i(x);  h h h h h h h h x; end
C> def j(x); i i i i  i i i   x;end

C> p (j(1) == j(1))

Here it take too long time to compute it.

Guy Decoux