From: Bharat Ruparel Date: 2007-02-15T05:40:30+09:00 Subject: Another Programming Ruby Question This is from Chapter 11 titled "Threads and Processes" of Programming Ruby book on page 139. I am pasting the source code below: # Sample code from Programing Ruby, page 131 $stderr.sync = $stdout.sync = true Thread.abort_on_exception = true threads = [] 4.times do |number| threads << Thread.new(number) do |i| raise "Boom!" if i == 2 print "#{i}\n" end end threads.each {|t| t.join } Note that the page numbers in the downloaded code with the actual page numbers do not match - probably due to two different Editions of the book. Has not been a problem thus far. When I run the code in SCITE, I get the following output: >ruby ex0318.rb ex0318.rb:7: Boom! (RuntimeError) from ex0318.rb:6:in `initialize' from ex0318.rb:6:in `new' from ex0318.rb:6 from ex0318.rb:5:in `times' from ex0318.rb:5 >Exit code: 1 Now the output shown in the book is different. It shows that a 0 followed by 1 are going to be output (on separate lines) before the program hits the exception condition. I commented out the line which says $stderr.sync = $stdout.sync = true as follows: # Sample code from Programing Ruby, page 131 # $stderr.sync = $stdout.sync = true Thread.abort_on_exception = true threads = [] 4.times do |number| threads << Thread.new(number) do |i| raise "Boom!" if i == 2 print "#{i}\n" end end threads.each {|t| t.join } and re-ran the program. This time I got the right output as below: >ruby ex0318.rb 0 1 ex0318.rb:7: Boom! (RuntimeError) from ex0318.rb:6:in `initialize' from ex0318.rb:6:in `new' from ex0318.rb:6 from ex0318.rb:5:in `times' from ex0318.rb:5 >Exit code: 1 That set me thinking what does the line: $stderr.sync = $stdout.sync = true actually do? I looked it up into the book and googled for it, but cannot find an answer that makes sense. Would someone kindly explain in Ruby/Newby terms? When to use it when not to use it etc? I know that this is setting both standard error and standard output to the same channel. What does the sync method do? What does it mean to set it to true? If there are online resources that can be looked up for these kinds of questions I will not bother you. But the online Ruby API almost mirrors Dave Thomas's Programming Ruby book which I am working with anyway. Thanks in advance for your time. Bharat -- Posted via http://www.ruby-forum.com/.