B
Bharat Ruparel
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:
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
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:
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
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
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:
ex0318.rb:7: Boom! (RuntimeError)ruby ex0318.rb
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:
0ruby ex0318.rb
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