【问题标题】:Ruby Script Error红宝石脚本错误
【发布时间】:2013-05-29 05:34:05
【问题描述】:

在程序下半部分的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

【问题讨论】:

    标签: ruby class methods argv


    【解决方案1】:

    因为你必须使用$stdin.gets,否则它会读取ARGV中给出的文件。

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

    【讨论】:

    • 感谢您的更正和链接今天要学习一些非常有趣的东西!
    猜你喜欢
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 2017-05-26
    相关资源
    最近更新 更多