From: ara.t.howard@... Date: 2006-02-03T04:39:21+09:00 Subject: Re: Chomping and stomping On Fri, 3 Feb 2006, James Edward Gray II wrote: > 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 golfing def chomper() STDIN.detect{|x| x =~ /^qq/ or puts "hit me"} end -a -- happiness is not something ready-made. it comes from your own actions. - h.h. the 14th dali lama