【发布时间】:2011-11-15 06:48:39
【问题描述】:
我想创建一个脚本,我可以通过管道将列表输入:cat list | ruby script.rb 我知道我可以通过使用ARGF 来做到这一点,并且它可以工作。但是话又说回来,我想遍历每一行并根据决定对其进行一些操作。但是当我使用STDIN.gets 时,我甚至不会在输入内容时被要求输入。
这就是我现在拥有的
#!/usr/bin/env ruby
# encoding: utf-8
items = []
ARGF.each_with_index do |line, index|
items << line
end
items.each_with_index do |item, index|
puts "What to do with #{item}? (1,2,3)"
case STDIN.gets
when 1
puts "one"
when 2
puts "two"
when 3
puts "three"
end
end
感谢您为我指明正确方向的任何提示。
【问题讨论】: