如何在带有参数的pry中运行文件

时间:2012-10-05 03:06:31

标签: ruby pry

我可以像这样开始一个命令行应用程序的pry会话

pry -r ./todo.rb

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

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 - %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

2 个答案:

答案 0 :(得分:6)

来自pry --help

  

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

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

pry -r ./todo.rb -e list


更新

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

require 'pry'; binding.pry

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

答案 1 :(得分:0)

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