leonel
leonel

Reputation: 10224

How to tell STDIN to stop reading?

So I am studying Build Awesome Command-Line Applications in Ruby. On page 81, we're supposed to use STDIN to enter more than one task into a project.

 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

The usual way to enter tasks into a project it's like this $todo new "Rake leaves but with the code above we can to what's in the example below.

It does work. But how do I tell STDIN to stop listening? The example on how to use it is this...

$ todo new
Rake leaves
Take out trash
Clean garage
Put away dishes
^D

What does the ^D represent?

Upvotes: 1

Views: 2825

Answers (2)

Jon Purdy
Jon Purdy

Reputation: 54999

It’s an end-of-file character. You can type this literally on Unix systems with Ctrl+D or on Windows with Ctrl+Z. The traditional way of displaying the Ctrl modifier is with a ^ prefix, e.g., ^D.

Be aware that this closes standard input entirely. If you want to read more data after entering these lines, you’ll need to check the input itself for a different delimiter—for instance, an empty line.

Upvotes: 5

sepp2k
sepp2k

Reputation: 370212

You can close STDIN by pressing Ctrl-d on Unix-like systems or Ctrl-z on Windows.

What does the ^6 represent?

Are you sure it says ^6 and not ^d? If so, that's probably a typo.

Upvotes: 4

Related Questions