Better irb : PRY ! pry, looksee, readline
- Better IRB - a PRY test
- PRY - basic stuff
- Pry Plugins
- Configure Rails with Pry
- PRY Tips
- Looksee gem
- My .pryrc
Better IRB - a PRY test
Install gem i pry
. When you launch it, you’ve warned by some readline missing gem. But take a look at this bug … the new version of rb-readline (0.5.pre) is not available (gem list -r rb-readline -a
). So :
- clone the depot
git clone https://github.com/luislavena/rb-readline.git
somewhere - just launch
ruby setup.rb
in this cloned directory (you can drop the clone after, and don’t install the gem for now, you’ll get some nasty bugs …) : some fresh readline library will be copied on your ruby 2.0 installation.
And it’s done (except for the warning “DL depreciated …”). Don’t bother for the win32console message, your ANSICON is working (test it : test-ansi
in your pry shell)
PRY - basic stuff
First, don’t forget tab and double tab completion !
help
for a list of common commandwtf?
… for the last plantage :)cd Array
change the binding for Array classls
list method from the bindingcd ..
back to the main binding (take a look further at looksee gem also) andnesting
for view where we’re from ; very useful for Rails railscastsimple-prompt
for toggle prompthist -tail 5
: list last commands ;hist --grep try
for listing command with try andhist --replay 70..72
for replaying lines 70 to 72 (work fine with just one line too).ls .pwd .mkdir ...
: you can access directly to all bash / dos command with a dot prefix ; another example (from pry wiki page) how to use ruby variables in shell command :
pry(main)> x = "[mM]"
=> "[mM]"
pry(main)> .ls | grep #{x}
find-method history
: to find a method declared in charged gems, add-c
arg option to see who’s calling this methodcat GemFile -l -s 10
: show the file with -l line number, starting from -s the 10th line. Remember you’re under some improved cat command for navigation around the file (q for quit, … )gem-cd rails
: the gem-cd command change the directory directly to the gem’s one.__out[23]__
: use the output result from the 23th command (__in[23]__
for the input command)edit myfile.rb
launch your editor ; now when you test ie
def hello
puts "hi !"
end
you can use edit hello
for invoking your editor upon your method, or just edit -t
for invoking a temp file
Pry Plugins
gem i 'pry-theme'
and in .pryrc
Pry.config.theme = "railscasts"
you can change on the fly in Pry with pry-theme try _themename_
Note that some theme are in 256 colors … and they don’t work well with windows, so no railscast theme running for the moment …
Configure Rails with Pry
Install the gem in the gemfile
gem i 'pry-rails', :group =>development
Usage :
show-routes
show-routes --grep new
show-models
PRY Tips
If you want a quiet return (don’t show the result), just put a
;
at the end of the pry line, ie :User.first
vs
User.first;
Usefull when calling a method formatting a result …
Looksee gem
gem i looksee
in .pryrc
require 'looksee' Usage :
>[].ls
>[].ls /^to/
My .pryrc
From several sources. I’ve try to colorize the prompt, but i still have a got a small bug.
Pry.config.auto_indent = true
Pry.config.color = true
Pry.config.editor = "vim"
Pry.config.history.file = "~/pry_history"
Pry.config.hooks.add_hook(:before_session, :say_hi_traz) do
puts "\n\nHi Traz !\nRuby#{RUBY_VERSION}\n\n"
end
Pry.config.hooks.add_hook(:after_session, :say_bye_traz) do
puts "\n\nbye-bye Traz !\n\n"
end
# color = {
# :red => "31m",
# :green => "32m",
# :yellow => "33m",
# :blue => "34m",
# :purple => "35m",
# :cyan => "36m"
# }
Pry.prompt = [
proc { |target_self, nest_level, pry|
"[#{pry.input_array.size}]\001\e[0;32m\002#{Pry.config.prompt_name}\001\e[0m\002(\001\e[0;33m\002#{Pry.view_clip(target_self)}\001\e[0m\002)#{":#{nest_level}" unless nest_level.zero?}> "
},
proc { |target_self, nest_level, pry|
"[#{pry.input_array.size}]\001\e[1;32m\002#{Pry.config.prompt_name}\001\e[0m\002(\001\e[1;33m\002#{Pry.view_clip(target_self)}\001\e[0m\002)#{":#{nest_level}" unless nest_level.zero?}* "
}
]
require 'rubygems'
require 'looksee'
# theme on win work only with 8 and 16 colors
# Pry.config.theme = "railscasts"
begin
require 'awesome_print'
AwesomePrint.pry!
#Pry.config.print = proc { |output, value| output.puts value.ai }
rescue LoadError => err
puts "no awesome_print :("
end
# require 'wirble'
# Wirble.init
# Wirble.colorize
begin
require 'hirb'
rescue LoadError
# Missing goodies, bummer
end
if defined? Hirb
# Dirty hack to support in-session Hirb.disable/enable
Hirb::View.instance_eval do
def enable_output_method
@output_method = true
Pry.config.print = proc do |output, value|
Hirb::View.view_or_page_output(value) || Pry::DEFAULT_PRINT.call(output, value)
end
end
def disable_output_method
Pry.config.print = proc { |output, value| Pry::DEFAULT_PRINT.call(output, value) }
@output_method = nil
end
end
Hirb.enable
end
comments powered by Disqus