4

このようなコマンドラインアプリのこじ開けセッションを開始できます

pry -r ./todo.rb

ただし、リスト関数を呼び出したい場合

pry -r ./todo.rb list 

エラーメッセージが表示されます。

こじ開けずに 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メソッドが on で定義されている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 に答える