Writing to STDOUT after closing controlling terminal



Hi,
I don't know if this is a dumb question or not (I hope it isn't), but it
seems to me there's something wrong here. I've been using Ruby for a
long time now, and I never noticed this (maybe it wasn't working like
this in the past), and it just seems odd.
If I write the following code:

sleep(5)
begin
puts 'hi!'
rescue
File.open('output', 'w') {|io| io.puts $!.message }
end

And execute it in Ruby 1.9.1p243 in a graphical terminal (e.g.: konsole)
as a foreground process I obviously get this:

$ ./test.rb
hi!

And there's no "output" file created sine no exception was raised.

Now, If I do:
$ ./test.rb &
[1] 28137
and immediately close konsole, 5 seconds later I get:
$ cat output
Input/output error - <STDOUT>

Is this ok? I seems to me that one shouldn't have to care if the
controlling terminal is closed when doing "puts" to avoid this exception
from ending my ruby process.

As a sidenote, with Ruby 1.8.7p174 the process doesn't get any
exception. If now I write (to check for normal process completion):

sleep(5)
begin
puts 'hi!'
File.open('output2', 'w') {|io| io.puts 'ok' }
rescue
File.open('output', 'w') {|io| io.puts $!.message }
end
with

and execute it as before, I get the "output2" file with the "ok" string
in it.

Is this a bug? a feature?

Thank you,
Matt
--
Posted via http://www.ruby-forum.com/.

.



Relevant Pages

  • Re: Writing to STDOUT after closing controlling terminal
    ... And execute it in Ruby 1.9.1p243 in a graphical terminal ... And there's no "output" file created sine no exception was raised. ... controlling terminal is closed when doing "puts" to avoid this exception ...
    (comp.lang.ruby)
  • Re: Proper use of ipaddr library?
    ... The string triggering the error is not a valid IP - neither IPv4 nor ... puts "Found Key for: #" ... I suggest you read a bit about exception handling in Ruby, ...
    (comp.lang.ruby)
  • Ruby script to Module/Class refactor
    ... I have written my first live ruby script that actually performs useful ... # and append .csv extension to each. ... puts FileTest.readable?? ...
    (comp.lang.ruby)
  • Re: Worth an RCR? static_type_check, polymorphic_type_check, quacks_like
    ... Keep in mind, though, that static type checking isn't just some safety ... net that Matz forgot to include in Ruby. ... Only much later when you invoke the methods on the object do ... class TypeCheckException < Exception ...
    (comp.lang.ruby)
  • Re: lowering rubys reach
    ... Don't get me wrong it should make it a lot easier to write extensions for ruby - especially those that are merely for speed gains. ... What I was think of was the introduction of language profiles which would trigger certain language features. ... would complain about variable a not being declared and would need to be change to include a forward declaration, ...
    (comp.lang.ruby)