From: Matt Armstrong Date: 2001-10-21T15:28:01+09:00 Subject: [ruby-talk:22863] Re: Newbie: Ruby equiv for Perl while (<>)??? Chris Olive writes: > I can't seem to find the Ruby syntax that is equivalent to the following > in Perl: > > while (<>) { DoAnyThing( $_ ) } ARGF.each { |l| DoAnything(l) } ARGF is a special object providing an API like a File object, but iterates over all files on the command line (or stdin). > ... I can just type in the line, and test at the command > line: > > $ perl -e 'while (<>) { if (/^\d/) { print "Starts with digit\n" } else > { print "Nope\n" } }' > 123 > Starts with digit > Hey! > Nope > ^D > > How can I do the same in Ruby? There's got to be a way! In addition to what David Alan Black suggested, there is the very explicit: ruby -e 'ARGF.each { |l| if l =~ /^\d/ then puts "Starts with digit" \ else puts "Nope" end }' -- matt