From: Albert Wagner Date: 2002-08-04T04:35:12+09:00 Subject: 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 ---------------------------------------------------------------------