From: James Edward Gray II Date: 2006-02-03T03:35:36+09:00 Subject: Re: Chomping and stomping On Feb 2, 2006, at 11:59 AM, Jacob Fugal wrote: > On 2/2/06, ara.t.howard@noaa.gov wrote: >> harp:~ > ruby a.rb < /dev/null >> a.rb:3:in `chomper': private method `chomp' called for >> nil:NilClass (NoMethodError) >> from a.rb:9 >> >> nil doesn't like that! ;-) > > Heh, yeah, I glossed over that too. You really need to check the > result of gets before you chomp it. Isn't this why we have iterators in Ruby? We get to avoid all these messy loop ending tests... > This is the way I'd write John's original loop (same semantics): > > def chomper > while xx = gets > xx.chomp! > break if xx == 'qq' > puts "hit me with a squirell!" > end > xx > end def chomper $stdin.each do |line| line.chomp! return line if line == "qq" puts "Hit me with a squirrel!" end end James Edward Gray II