<p>I want to deal with the command line input in Ruby:</p>

<pre><code>&gt; cat input.txt | myprog.rb
&gt; myprog.rb &lt; input.txt
&gt; myprog.rb arg1 arg2 arg3 ...
</code></pre>

<p>What is the best way to do it? In particular I want to deal with blank STDIN, and I hope for an elegant solution.</p>

<pre><code>#!/usr/bin/env ruby

STDIN.read.split("\n").each do |a|
   puts a
end

ARGV.each do |b|
    puts b
end
</code></pre>

I am not quite sure what you need, but I would use something like this:
```
```
#!/usr/bin/env ruby

until ARGV.empty? do
  puts "From arguments: #{ARGV.shift}"
end

while a = gets
  puts "From stdin: #{a}"
end
```
```
Note that because ARGV array is empty before first
```
gets
```
, Ruby won't try to interpret argument as text file from which to read (behaviour inherited from Perl).
If stdin is empty or there is no arguments, nothing is printed.
Few test cases:
```
```
$ cat input.txt | ./myprog.rb
From stdin: line 1
From stdin: line 2

$ ./myprog.rb arg1 arg2 arg3
From arguments: arg1
From arguments: arg2
From arguments: arg3
hi!
From stdin: hi!
```
```