我想在Ruby中处理命令行输入:
> cat input.txt | myprog.rb
> myprog.rb < input.txt
> myprog.rb arg1 arg2 arg3 ...
最好的方法是什么?我特别想处理空白的STDIN,我希望有一个优雅的解决方案。
#!/usr/bin/env ruby
STDIN.read.split("\n").each do |a|
puts a
end
ARGV.each do |b|
puts b
end
我想在Ruby中处理命令行输入:
> cat input.txt | myprog.rb
> myprog.rb < input.txt
> myprog.rb arg1 arg2 arg3 ...
最好的方法是什么?我特别想处理空白的STDIN,我希望有一个优雅的解决方案。
#!/usr/bin/env ruby
STDIN.read.split("\n").each do |a|
puts a
end
ARGV.each do |b|
puts b
end
当前回答
你也可以使用STDIN。each_line和STDIN.each_line。To_a将其作为数组获取。
e.g.
STDIN.each_line do |line|
puts line
end
其他回答
也许是这样的?
#/usr/bin/env ruby
if $stdin.tty?
ARGV.each do |file|
puts "do something with this file: #{file}"
end
else
$stdin.each_line do |line|
puts "do something with this line: #{line}"
end
end
例子:
> cat input.txt | ./myprog.rb
do something with this line: this
do something with this line: is
do something with this line: a
do something with this line: test
> ./myprog.rb < input.txt
do something with this line: this
do something with this line: is
do something with this line: a
do something with this line: test
> ./myprog.rb arg1 arg2 arg3
do something with this file: arg1
do something with this file: arg2
do something with this file: arg3
Ruby提供了另一种处理STDIN的方法:-n标志。它将整个程序视为在STDIN上的循环中(包括作为命令行参数传递的文件)。参见以下一行脚本:
#!/usr/bin/env ruby -n
#example.rb
puts "hello: #{$_}" #prepend 'hello:' to each line from STDIN
#these will all work:
# ./example.rb < input.txt
# cat input.txt | ./example.rb
# ./example.rb input.txt
我不太清楚你需要什么,但我会用这样的东西:
#!/usr/bin/env ruby
until ARGV.empty? do
puts "From arguments: #{ARGV.shift}"
end
while a = gets
puts "From stdin: #{a}"
end
注意,因为ARGV数组在第一次获取之前是空的,Ruby不会尝试将参数解释为要读取的文本文件(行为继承自Perl)。
如果stdin为空或没有参数,则不打印任何内容。
很少的测试用例:
$ 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!
我是这样做的:
all_lines = ""
ARGV.each do |line|
all_lines << line + "\n"
end
puts all_lines
while STDIN.gets
puts $_
end
while ARGF.gets
puts $_
end
这是受到Perl的启发:
while(<STDIN>){
print "$_\n"
}