Pry is the enhancement of Ruby REPL irb
,
equivalent of Python's IPython.
Install, start and show help
Check and install:
gem search pry
gem info -b pry # search Both local and remote, by default only local
gem install pry pry-doc pry-byebug pry-theme awesome_print coderay
Note: gem install pry-full
failed with dependency error
.
Start Pry inside irb and show help:
> require 'pry'
> binding.pry
> help
Frequently Used Commands
hist # show command history
.pwd # run shell command with `.` prefix
cd myobj1 # go into an object
whereami # print the context where the debugger is stopped
ls # list methods of `myobj`
method1 # equals to `myobj1.method1`
cd .. # go out of myobj1
reset # clear pry environment
Toggle shell-mode with shell-mode
.
Show doc: ? method-name
or show-doc ...
, ri ...
.
Show source codes of a method: $ method-name
or show-source ...
.
Run shell command with .
prefix.
Define new command or aliases
The following command add some aliases and a new command into Pry session:
cat << EOF >> ~/.pryrc
Pry.editor = 'nvim'
Pry.config.commands.alias_command "w", "whereami"
Pry.config.commands.alias_command "n", "next"
Pry.config.commands.alias_command "c", "continue"
Pry.config.commands.alias_command "b", "break"
Pry.config.commands.alias_command "s", "step"
Pry.config.commands.alias_command "bt", "backtrace"
Pry.config.commands.command "l", "Load a Ruby file into current session" do |filename|
Pry.toplevel_binding.eval File.read("#{filename}.rb")
end
EOF
Debug
Debug a ruby script by adding a binding.pry
at the debugging point,
like set_trace()
for IPython.
To debug a script, run ruby myscript.rb
in shell,
or load 'myscript.rb'
in pry REPL.
It will be stopped at the line binding.pry
.
Then you can use next
, continue
, etc to debug the script.
Using help
in pry REPL to list available commands.
All aliases defined in ~/.pryrc are listed in section "Aliases".
All debugging commands are listed in section "Byebug".
See pry-byebug for details.
A minimum demo script:
require 'pry'
binding.pry
class Number < Struct.new(:value)
def to_s
value.to_s
end
end
n = Number.new(53)
puts(n.to_s)
Run pry-theme list
in pry REPL to show all themes.
Ref:
Notes
To open tab completion and auto indention in irb, add the following lines into ~/.irbrc:
require 'irb/completion'
IRB.conf[:AUTO_INDENT] = true
Or using Bond, gem install bond
and add the following into ~/.irbrc:
require 'bond'
Bond.start