How to run a file in pry that takes arguments

I can start a pry session of a command line application like this

pry -r ./todo.rb 

However, if I want to call a list function

 pry -r ./todo.rb list 

I get an error message.

Without pry, I call a list function

 ruby todo.rb list 

This error message

 /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>' 

Source

 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 

Update

when i try

 pry -r ./todo.rb -e list 

I get the following error

 NameError: undefined local variable or method `list' for main:Object 
+4
source share
2 answers

From pry --help :

-e, --exec A line of code to execute in context before starting a session

So, if your list method is defined in main (if you don't know this, maybe it is), you can do this:

pry -r ./todo.rb -e list


Update

Pry does not allow passing arguments for scripts loaded by it (or, at least, is not documented). But all is not lost, you can call pry from your script. Just leave this where you want to check:

 require 'pry'; binding.pry 

This will result in a pry session that has access to all local variables and methods.

+6
source

I think you can use:
ruby -rpry ./todo.rb -e list

0
source

Source: https://habr.com/ru/post/1437925/


All Articles