awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

Stripe gem incompatible with awesome_print #306

Closed fatuhoku closed 7 years ago

fatuhoku commented 7 years ago
> p Stripe::Transfer.retrieve("some_valid_transfer_id")
#<Stripe::Transfer:0x3fc9e98156e4 id=some_valid_transfer_id> JSON: {
  "id": "some_valid_transfer_id",
  "object": "transfer",
  ... # blah blah blah blah
}
(pry) output error: #<ArgumentError: wrong number of arguments (1 for 0)>

> ap Stripe::Transfer.retrieve("some_valid_transfer_id")
ArgumentError: wrong number of arguments (1 for 0)
from ~/.rvm/gems/ruby-2.2.4/gems/stripe-2.1.0/lib/stripe/stripe_object.rb:241:in `block (3 levels) in add_accessors'
jakeonrails commented 7 years ago

Do you have more backtrace? Esp. any lines that are actually from awesome_print?

fatuhoku commented 7 years ago

Nope. You can see from the lines I've posted that the problem doesn't exist with p and does for ap. As a user of awesome-print and Stripe I have no idea how the guts of either have caused this bizarre error. I mean, how could I?

jakeonrails commented 7 years ago
ArgumentError: wrong number of arguments (given 1, expected 0)
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/stripe-1.55.0/lib/stripe/stripe_object.rb:231:in `block (3 levels) in add_accessors'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/awesome_print-1.7.0/lib/awesome_print/formatter.rb:122:in `convert_to_hash'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/awesome_print-1.7.0/lib/awesome_print/formatter.rb:55:in `awesome_self'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/awesome_print-1.7.0/lib/awesome_print/formatter.rb:36:in `format'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/awesome_print-1.7.0/lib/awesome_print/inspector.rb:148:in `unnested'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/awesome_print-1.7.0/lib/awesome_print/inspector.rb:115:in `awesome'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/awesome_print-1.7.0/lib/awesome_print/core_ext/kernel.rb:10:in `ai'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/awesome_print-1.7.0/lib/awesome_print/core_ext/kernel.rb:20:in `ap'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/awesome_print-1.7.0/lib/awesome_print/inspector.rb:33:in `output_value'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/railties-4.2.8/lib/rails/commands/console.rb:110:in `start'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/railties-4.2.8/lib/rails/commands/console.rb:9:in `start'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `require'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `block in require'
... 3 levels...
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
    from /Users/jakemoffatt/.rvm/gems/ruby-2.3.3@mobile-notary/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /Users/jakemoffatt/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/jakemoffatt/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

https://github.com/awesome-print/awesome_print/blob/master/lib/awesome_print/formatter.rb#L109-L124

this method calls method(:to_hash) and that causes the exception in Stripe::StripeObject in add_accessors. I can't tell why calling .method(:to_hash) would result in a call to add_accessors, just yet.

This is certainly an issue w/ Stripe's metaprogramming in Stripe::StripeObject, it's not a problem with awesome_print.

https://github.com/stripe/stripe-ruby/blob/master/lib/stripe/stripe_object.rb#L232-L259

In the meantime you could call to_hash directly and ap transfer.to_hash which will serve as a workaround. I've tested locally.

jakeonrails commented 7 years ago

Ok, I think I figured it out.

Stripe::Transfer has a method attribute. See here: Transfer#method

The Stripe::StripeObject class is a neat little metaprogrammed thing that takes a hash as input then automatically adds accessors for each of the attributes of the hash, in a nested fashion.

Sadly, though, method is a default method on every Ruby object, so their choosing method as an attribute of Stripe::Transfer causes a conflict.

Awesome print just so happens to rely on the method method to work as expected, and Stripe's API breaks it. This issue should be opened on the Stripe gem's Github with a link back here for reference.

fatuhoku commented 7 years ago

Really interesting findings @jakeonrails thanks for looking into it. I'll make that issue at Stripe and link back as you suggested.

fatuhoku commented 7 years ago

Closing as dupe of stripe/stripe-ruby#524