From: Jim Freeze Date: 2002-05-09T08:31:49+09:00 Subject: Re: Want application to read file or from pipe On Thu, May 09, 2002 at 06:59:10AM +0900, Tim Hammerquist wrote: > Jim Freeze graced us by uttering: > > $ cat filter.rb > #!/usr/local/bin/ruby -w > > while gets > # each line given in $_ for each iteration > puts "you gave me: #$_" > end Close, but I am looking for something without the while and gets statements. Something more like readlines. Here is my meager attempt: cat pipe.rb #! /usr/local/bin/ruby if 1 == ARGV.size lines = IO.readlines(ARGV[0]) else lines = $stdin.read.split(/\n/) end p lines jfreeze@rabbit ('tty') ~/tmp 29 -> cat pipe.rb | ./pipe.rb ["#! /usr/local/bin/ruby", "", "if 1 == ARGV.size", " lines = IO.readlines(ARGV[0])", "else", " lines = $stdin.read.split(/\\n/)", "end", "", "p lines"] jfreeze@rabbit ('tty') ~/tmp 30 -> ./pipe.rb pipe.rb ["#! /usr/local/bin/ruby\n", "\n", "if 1 == ARGV.size\n", " lines = IO.readlines(ARGV[0])\n", "else\n", " lines = $stdin.read.split(/\\n/)\n", "end\n", "\n", "p lines\n"] They are close, but splitting on \n is asking for trouble. Is there a better way to do this? -- Jim Freeze If only I had something clever to say for my comment... ~