4

我可以像这样启动命令行应用程序的撬动会话

pry -r ./todo.rb

但是,如果我想调用列表函数

pry -r ./todo.rb 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 - %s\n",counter,name)
      printf("      Created   : %s\n",created)
      unless completed.nil?
        printf("      Completed : %s\n",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,created,completed = read_todo(line)
        if task_number == counter
          write_todo(new_file,name,created,Time.now)
          puts "Task #{counter} completed"
        else
          write_todo(new_file,name,created,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
4

2 回答 2

6

来自pry --help

-e, --exec会话开始前在上下文中执行的一行代码

因此,如果您的list方法已定义main(如果您不知道,可能是),那么您可以这样做:

pry -r ./todo.rb -e list


更新

Pry 不允许您为它加载的脚本传递参数(或者至少它没有记录)。但一切都没有丢失,你可以pry从你的脚本中调用。只需将其放在您要检查的任何地方:

require 'pry'; binding.pry

这将产生一个可以访问所有局部变量和方法的 pry 会话。

于 2012-10-05T03:16:49.320 回答
0

我认为你可以使用:
ruby -rpry ./todo.rb -e list

于 2015-01-07T08:22:26.357 回答