Better irb : PRY ! pry, looksee, readline

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 :

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 !

    pry(main)> x = "[mM]"
    => "[mM]"
    pry(main)> .ls | grep #{x}
def hello
  puts "hi !"
end

you can use edit hellofor 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