From: Christoph Rippel Date: 2000-11-22T14:41:30+09:00 Subject: [ruby-talk:6513] Question about the behavior of write attributes in blocks Hi, I am trying to understand iterators and blocks in Ruby. Could someone explain to me the logic behind the output >try fail fail >succ succ succ of the following program? How can I fix my tree class to get the ``right'' behavior? *********************** # A binary tree class class Bt def initialize( cont, l = nil, r = nil ) @data=cont @left=l @right=r end def each yield data left.each { |l| yield l } right.each { |r| yield r } end attr_accessor( :left, :right, :data) end module Each def each end end nil.extend(Each) root = Bt.new ( "fail ", Bt.new "fail ", Bt.new "fail ") root.data="try " # ``data'' is changed root.each { |a| a = "try " } # ``data'' is not changed root.each { |a| print a } puts root.each { |a| a.replace "succ " } # ``data'' is changed root.each { |a| print a } ***************************************** Thanks, Christoph