Subtle bug in Ruby/Tk? (Attn: Hidetoshi Nagai)

In the sample code below, a subtle bug appears in either Ruby/Tk or Tk itself.
Within the class Node, when a recursive call is made to method descendTree,
the to_s method is apparently invoked within the Tk.mainloop, only if to_s
does not return a string. I know that to_s should return a string, but why
is to_s even being called? And why is it being called only when descendTree
is called recursively? The code can be made to work correctly if (1) in
to_s, comment out —puts “to_s”—and replace with —“to_s”— OR (2)
comment out the addChild statement so that descendTree is not called
recursively.

···

require ‘tk’

class Node
def initialize(name)
@name = name
@children = Array.new
end

def addChild(aNode)
	@children.push(aNode)
end
attr_reader(:name, :children)

def to_s
	puts "to_s" #comment this out and
	#"to_s"		# uncomment this
end

end

def aProc
showTree
end

def showTree
descendTree(0, @tree)
end

def descendTree(level, aNode)
puts “#{”\t"*level} #{aNode.name}"
aNode.children.each do |node|
descendTree(level + 1, node)
end
end

@tree = Node.new(‘top’)
@tree.addChild(Node.new(‘A’)) #comment this out

root = TkRoot.new
menuFrame = TkFrame.new(root).pack
menuButton = TkMenubutton.new(menuFrame,‘text’=>‘Button’).pack
menu = TkMenu.new(menuButton)
menuButton.configure(‘menu’=>menu)
menu.add(‘command’,‘label’=>‘Item’,‘command’=>proc{aProc})
Tk.mainloop

Hi,

···

From: Albert Wagner <alwagner@tcac.net>
Subject: Subtle bug in Ruby/Tk? (Attn: Hidetoshi Nagai)
Date: Sat, 3 Aug 2002 14:22:54 -0500
Message-ID: <200208031422.54170.alwagner@tcac.net>

In the sample code below, a subtle bug appears in either Ruby/Tk or Tk itself.

I'm very sorry, but I cannot maintain Ruby/Tk now.
I got damage on my left eye. My doctor told me to minimize the fatigue
on my eyes for one or two months to avoid the detached retina.
And my main work is enough to fatigue my eyes. :slight_smile:
--
                                     H. NAGAI (nagai@ai.kyutech.ac.jp)

nagai@ai.kyutech.ac.jp wrote:

Hi,

From: Albert Wagner alwagner@tcac.net
Subject: Subtle bug in Ruby/Tk? (Attn: Hidetoshi Nagai)
Date: Sat, 3 Aug 2002 14:22:54 -0500
Message-ID: 200208031422.54170.alwagner@tcac.net

In the sample code below, a subtle bug appears in either Ruby/Tk or Tk itself.

I’m very sorry, but I cannot maintain Ruby/Tk now.
I got damage on my left eye. My doctor told me to minimize the fatigue
on my eyes for one or two months to avoid the detached retina.
And my main work is enough to fatigue my eyes. :slight_smile:

Oh my. I hope it all goes well.

···


Giuseppe “Oblomov” Bilotta

Axiom I of the Giuseppe Bilotta
theory of IT:
Anything is better than MS

I’m very sorry, but I cannot maintain Ruby/Tk now.
I got damage on my left eye. My doctor told me to minimize the fatigue
on my eyes for one or two months to avoid the detached retina.
And my main work is enough to fatigue my eyes. :slight_smile:

I am very sorry to hear that. Best of luck with your eye!

From: Albert Wagner alwagner@tcac.net
Subject: Subtle bug in Ruby/Tk? (Attn: Hidetoshi Nagai)

Your to_s returns nil (puts returns nil, not the string it prints),
which it is not supposed to do: it should return a String.

Somehow, the result of the proc is passed to Tk (which is a Node!) as a
string (that’s how Ruby and Tk communicate: via tcl, in strings).

Now the question is, what does Tk need the result for, to which I don’t
have a clear answer. If it’s like Ruby, tcl returns a value, which might be
intersting (a Tk object, in the case of the Ruby/Tk bindings). I’d have to
dig in more code than I want to find out.

Even though you’re returning a Node and not a Tk object, I’m inclined to
say the bug is yours, Albert.

Bye,
Kero.

±-- Kero ------------------------------ kero@chello.nl —+

Don’t split your mentality without thinking twice |
Proud like a God – Guano Apes |
±-- M38c ---------- http://httpd.chello.nl/k.vangelder —+