0

在程序下半部分的gets部分,终端不是要求我输入而是自动接受输入。我无法理解这是如何发生的。

该代码是:

puts "Welcome to my automatic file opener"
puts "Version - 2.0"

if ARGV[0] && ARGV[1] #to ensure Arguements are given as input
old_data = File.open(ARGV[0]).readlines
new_data = File.open(ARGV[1]).readlines

class Differentiator

def old_stuff
  puts "the old files are:-"
  puts old_data
end

def new_stuff
  puts "The new files are:-"
  puts new_data
end

def updated_list
  puts "The newly added files  are:-"
  newly_added = new_data - old_data
  puts newly_added
end

def deleted_list
  puts "The deleted files are:-"
  deleted_data = old_data - new_data
  puts deleted_data
end

def stable_list
  puts "The unchanged/stable files are:-"
  unchanged_data = new_data - newly_added
  puts unchanged_data
end
end#end of class

while true
  puts "Choose your option:"
  puts "1.Old Files of System"
  puts "2.New Files of System"
  puts "3.Added Files of System"
  puts "4.Deleted Files of System"
  puts "5.Stable Lists"
  puts "6.Exit"
  print " Please Choose your Output:-"
  **option_method=gets.chomp.to_i**
  filecase1 = Differentiator.new

  if option_method == 1
  filecase1.old_stuff
  end

  if option_method == 2
  filecase1.new_stuff
  end

  if option_method == 3
  filecase1.updated_list
  end

  if option_method == 4
  filecase1.deleted_list
  end

  if option_method == 5
  filecase1.stable_list
  end

  if option_method == 6
  break
  exit
  end

  if option_method != (1..6)
  puts "Sorry,Wrong Input"
  end
end

else
 puts "The Right Method of Usage is :  ruby <scriptname>.rb old_file new_file"; exit;
end
4

1 回答 1

0

因为您必须使用,$stdin.gets否则它将读取ARGV.

它写在手册页http://www.ruby-doc.org/core-2.0/Kernel.html的第一行中谈论gets

于 2013-05-29T05:36:52.673 回答