ruby – 如何在带有参数的pry中运行文件
发布时间:2020-12-17 03:03:57 所属栏目:百科 来源:网络整理
导读:我可以像这样开始一个命令行应用程序的pry会话 pry -r ./todo.rb 但是,如果我想调用list函数 pry -r ./todo.rb list 我收到一条错误消息. 没有pry,我调用list函数 ruby todo.rb list 这是错误消息 /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@glo
我可以像这样开始一个命令行应用程序的pry会话
pry -r ./todo.rb 但是,如果我想调用list函数 pry -r ./todo.rb list 我收到一条错误消息. 没有pry,我调用list函数 ruby todo.rb list 这是错误消息 /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/lib/pry/repl_file_loader.rb:16:in `initialize': No such file: /Users/michaeljohnmitchell/Sites/todo/bin/list (RuntimeError) from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/lib/pry/pry_class.rb:161:in `new' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/lib/pry/pry_class.rb:161:in `load_file_through_repl' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/lib/pry/cli.rb:162:in `block in <top (required)>' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/lib/pry/cli.rb:65:in `call' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/lib/pry/cli.rb:65:in `block in parse_options' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/lib/pry/cli.rb:65:in `each' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/lib/pry/cli.rb:65:in `parse_options' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/gems/pry-0.9.10/bin/pry:16:in `<top (required)>' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/bin/pry:19:in `load' from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@global/bin/pry:19:in `<main>' 源代码 TODO_FILE = 'todo.txt' def read_todo(line) line.chomp.split(/,/) end def write_todo(file,name,created=Time.now,completed='') file.puts("#{name},#{created},#{completed}") end command = ARGV.shift case command when 'new' new_task = ARGV.shift File.open(TODO_FILE,'a') do |file| write_todo(file,new_task) puts "Task added." end when 'list' File.open(TODO_FILE,'r') do |file| counter = 1 file.readlines.each do |line| name,created,completed = read_todo(line) printf("%3d - %sn",counter,name) printf(" Created : %sn",created) unless completed.nil? printf(" Completed : %sn",completed) end counter += 1 end end when 'done' task_number = ARGV.shift.to_i binding.pry File.open(TODO_FILE,'r') do |file| File.open("#{TODO_FILE}.new",'w') do |new_file| counter = 1 file.readlines.each do |line| name,completed = read_todo(line) if task_number == counter write_todo(new_file,Time.now) puts "Task #{counter} completed" else write_todo(new_file,completed) end counter += 1 end end end `mv #{TODO_FILE}.new #{TODO_FILE}` end 更新 当我尝试 pry -r ./todo.rb -e list 我收到以下错误 NameError: undefined local variable or method `list' for main:Object 解决方法
来自pry –help:
所以,如果你的list方法是在main上定义的(如果你不知道,可能是),那么你可以这样做: pry -r ./todo.rb -e list 更新 Pry不允许你为它加载的脚本传递参数(或者至少它没有记录).但一切都没有丢失,你可以从脚本中调用pry.只需将其放在您要检查的任何位置: require 'pry'; binding.pry 这将生成一个可以访问所有局部变量和方法的pry会话. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |