From: Dave Thomas Date: 2001-08-24T22:13:57+09:00 Subject: [ruby-talk:20285] Re: Example 1: Was Ruby Article "Joshua Drake" writes: > > > > I guess my main comments would be: > > > > 1. It crashes if the user enters an end of file > > Do you mean if they hit ctrl-d? Or when they type END. When I run the > script, > it does not crash if I type END. If you are talking about ctrl-d... how > would I catch that? 'gets' returns nil on end of file (probably ^d on your box). That's why you see code like break unless line = gets or line = gets ? $_.chop : "END" or line = (gets || "END").chomp These all work because when Ruby tests a value in a boolean context, 'nil' is interpreted as false. > > 2. It should probably be chop, not chop! If you want to modify the line in place, use chop!, which changes the receiver: before: line = "hello\n" line.chop! after: line = "hello" The '!' indicates that the receiver is likely to be changed. If instead you want to return the value of the text without the last character, use 'chop' fred = line.chop This leaves 'line' unaltered: fred receives the new value. Also, it's probably safer to use 'chomp' rather than 'chop'. Regards Dave