From 10efa1d60a463b05f14a4d2d1e73d2621cfa567c Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Thu, 6 Sep 2012 18:37:51 -0700 Subject: [PATCH] Added AwesomePrint.irb! and AwesomePrint.pry! convenience methods --- README.md | 20 ++------------------ lib/awesome_print/inspector.rb | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 3fcc45e..7f4d858 100644 --- a/README.md +++ b/README.md @@ -232,31 +232,15 @@ Supported color names: To use awesome_print as default formatter in irb and Rails console add the following code to your ~/.irbrc file: - require "rubygems" require "awesome_print" - - unless IRB.version.include?('DietRB') - IRB::Irb.class_eval do - def output_value - ap @context.last_value - end - end - else # MacRuby - IRB.formatter = Class.new(IRB::Formatter) do - def inspect_object(object) - object.ai - end - end.new - end + AwesomePrint.irb! ### PRY integration ### If you miss awesome_print's way of formatting output, here's how you can use it in place of the formatting which comes with pry. Add the following code to your ~/.pryrc: - require "rubygems" require "awesome_print" - - Pry.print = proc { |output, value| output.puts value.ai } + AwesomePrint.pry! ### Logger Convenience Method ### awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 9cd99a0..eeb0b5e 100755 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -22,6 +22,29 @@ module AwesomePrint def rails_console? console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"]) end + + def irb! + return unless defined?(IRB) + unless IRB.version.include?("DietRB") + IRB::Irb.class_eval do + def output_value + ap @context.last_value + end + end + else # MacRuby + IRB.formatter = Class.new(IRB::Formatter) do + def inspect_object(object) + object.ai + end + end.new + end + end + + def pry! + if defined?(Pry) + Pry.print = proc { |output, value| output.puts value.ai } + end + end end class Inspector