所以我正在学习Ruby中的Build Awesome命令行应用程序。在第81页,我们应该使用STDIN将多个任务输入到一个项目中。如何告诉STDIN停止阅读?
File.open(global_options[:filename], 'a+') do |todo_file|
if task_names.empty?
puts "Reading new tasks from stdin..."
task_names = STDIN.readlines.map {|a| a.chomp}
end
tasks = 0
task_names.each do |task|
todo_file.puts [task, Time.now].join(', ')
tasks+=1
end
if tasks == 0
raise "You must provide tasks on the command-line or standard input"
end
end
通常的方式进入任务到项目是这样的$todo new "Rake leaves
但上面的代码,我们可以到什么是在下面的例子。
它确实有效。但是,我如何告诉STDIN停止收听?关于如何使用它的例子是...
$ todo new
Rake leaves
Take out trash
Clean garage
Put away dishes
^D
^D
代表什么?