Ruby中STDIN的最佳实践?

gri*_*let 299 ruby stdin

我想处理Ruby中的命令行输入:

> cat input.txt | myprog.rb
> myprog.rb < input.txt
> myprog.rb arg1 arg2 arg3 ...
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?特别是我想处理空白STDIN,我希望有一个优雅的解决方案.

#!/usr/bin/env ruby

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

ARGV.each do |b|
    puts b
end
Run Code Online (Sandbox Code Playgroud)

Jon*_*nke 394

以下是我在晦涩的Ruby系列中发现的一些东西.

因此,在Ruby中,Unix命令的一个简单的no-bells实现cat将是:

#!/usr/bin/env ruby
puts ARGF.read
Run Code Online (Sandbox Code Playgroud)

ARGF在输入方面是你的朋友; 它是一个虚拟文件,从命名文件或STDIN中获取所有输入.

ARGF.each_with_index do |line, idx|
    print ARGF.filename, ":", idx, ";", line
end

# print all the lines in every file passed via command line that contains login
ARGF.each do |line|
    puts line if line =~ /login/
end
Run Code Online (Sandbox Code Playgroud)

谢天谢地,我们没有得到Ruby中的钻石操作符,但我们确实得到ARGF了替代品.虽然模糊不清,但它实际上证明是有用的.考虑一下这个程序,该程序-i在命令行中提到的每个文件上就地存在版权标题(感谢另一个Perlism ):

#!/usr/bin/env ruby -i

Header = DATA.read

ARGF.each_line do |e|
  puts Header if ARGF.pos - e.length == 0
  puts e
end

__END__
#--
# Copyright (C) 2007 Fancypants, Inc.
#++
Run Code Online (Sandbox Code Playgroud)

归功于:

  • ARGF是要走的路.这是Ruby内置的以全方位处理文件和标准输入的方式. (12认同)

小智 41

Ruby提供了另一种处理STDIN的方法:-n标志.它将整个程序视为在STDIN上的循环中(包括作为命令行args传递的文件).请参阅以下1行脚本:

#!/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
Run Code Online (Sandbox Code Playgroud)

  • 三部分shebang`#!/ usr/bin/env ruby​​ -n`将无效,因为"ruby -n"将作为唯一参数传递给/ usr/bin/env.[查看此答案](http://stackoverflow.com/a/4304187)了解更多详情.如果明确地使用`ruby -n script.rb`运行,脚本**将会起作用. (7认同)
  • @jdizzle:它适用于OSX,但不适用于Linux - 而这正是问题所在:它不是_portable_. (5认同)
  • 包括'-n'在内的shebaing适用于我的Mac OS X 10.8盒子. (2认同)

Dam*_*kić 32

我不太确定你需要什么,但我会用这样的东西:

#!/usr/bin/env ruby

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

while a = gets
  puts "From stdin: #{a}"
end
Run Code Online (Sandbox Code Playgroud)

请注意,因为ARGV数组在第一次之前是空的gets,所以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!
Run Code Online (Sandbox Code Playgroud)


Mag*_*olm 18

也许这样的事情?

#/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
Run Code Online (Sandbox Code Playgroud)

例:

> 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
Run Code Online (Sandbox Code Playgroud)


tex*_*uce 12

while STDIN.gets
  puts $_
end

while ARGF.gets
  puts $_
end
Run Code Online (Sandbox Code Playgroud)

这是受Perl的启发:

while(<STDIN>){
  print "$_\n"
}
Run Code Online (Sandbox Code Playgroud)

  • 地狱啊,因为简单和可读性!哦不,等等,那是什么'$ _'?请在Stack Overflow上使用[English](http://www.ruby-doc.org/stdlib-2.0/libdoc/English/rdoc/English.html)! (4认同)