Hi,

I'm just getting started with Ruby, and I've run into a head-scratcher. I have the following code:

Code ruby:
class Node < Array
  def to_s
    puts(">>")
    tokens = self.each { |token|
      puts("$ " + token.to_s)
      token.to_s
    }
    puts("<<")
    "(" + tokens.join(" ") + ")"
  end
end
 
n = Node.new
n.push(1)
n.push(2)
m = Node.new
m.push(3)
m.push(4)
n.push(m)
puts(n.to_s)
I would've expected this output:
Code:
>>
$ 1
$ 2
>>
$ 3
$ 4
<<
$ (3 4)
<<
(1 2 (3 4))
But instead, I get this:
Code:
>>
$ 1
$ 2
>>
$ 3
$ 4
<<
$ (3 4)
>>
$ 3
$ 4
<<
<<
(1 2 3 4)
Any ideas?