diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a9d72e..05df835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ## master (unreleased) - + - Handles NoMethodError for IRB implicit `ai` [@jtnegrotto] - [#212] ## 1.7.0 - Refactoring by extracting formatters into their own classes [@waldyr] - [#237] @@ -129,6 +129,7 @@ - Initial Release. [#200]: https://github.com/awesome-print/awesome_print/pull/200 + [#212]: https://github.com/awesome-print/awesome_print/pull/212 [#216]: https://github.com/awesome-print/awesome_print/pull/216 [#217]: https://github.com/awesome-print/awesome_print/pull/217 [#222]: https://github.com/awesome-print/awesome_print/pull/222 @@ -145,6 +146,7 @@ [@clonezone]: https://github.com/clonezone [@cyberdelia]: https://github.com/cyberdelia [@gerrywastaken]: https://github.com/gerrywastaken + [@jtnegrotto]: https://github.com/jtnegrotto [@kemmason]: https://github.com/kemmason [@maurogeorge]: https://github.com/maurogeorge [@MaxPleaner]: https://github.com/MaxPleaner diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 30a269f..4d0ee71 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -31,6 +31,8 @@ module AwesomePrint IRB::Irb.class_eval do def output_value ap @context.last_value + rescue NoMethodError + puts "(Object doesn't support #ai)" end end else # MacRuby @@ -55,7 +57,7 @@ module AwesomePrint AP = :__awesome_print__ def initialize(options = {}) - @options = { + @options = { :indent => 4, # Indent using 4 spaces. :index => true, # Display array indices. :html => false, # Use ANSI color codes rather than HTML. @@ -65,7 +67,7 @@ module AwesomePrint :sort_keys => false, # Do not sort hash keys. :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. :new_hash_syntax => false, # Use the JSON like syntax { foo: 'bar' }, when the key is a symbol - :color => { + :color => { :args => :pale, :array => :white, :bigdecimal => :blue, @@ -104,7 +106,7 @@ module AwesomePrint def increase_indentation indentator.indent(&Proc.new) end - + # Dispatcher that detects data nesting and invokes object-aware formatter. #------------------------------------------------------------------------------ def awesome(object) diff --git a/spec/misc_spec.rb b/spec/misc_spec.rb index a26c702..8539470 100644 --- a/spec/misc_spec.rb +++ b/spec/misc_spec.rb @@ -246,5 +246,17 @@ EOS expect(capture! { ap({ :a => 1 }) }).to eq(nil) Object.instance_eval{ remove_const :IRB } end + + it "handles NoMethodError on IRB implicit #ai" do + module IRB; class Irb; end; end + irb_context = double('irb_context', last_value: BasicObject.new) + IRB.define_singleton_method :version, -> { 'test_version' } + irb = IRB::Irb.new + irb.instance_eval { @context = irb_context } + AwesomePrint.irb! + expect(irb).to receive(:puts).with("(Object doesn't support #ai)") + expect { irb.output_value }.to_not raise_error + Object.instance_eval { remove_const :IRB } + end end end