This project is archived and is in readonly mode.

#3622 ✓wontfix
Greg Hazel

exception_class.to_json == "{\"inheritable_attributes\":{}}"

Reported by Greg Hazel | December 27th, 2009 @ 07:24 AM

The way to_json treats classes is strange, and new in 2.3 (this worked as expected in 2.2):

StandardError.to_json == "{\"inheritable_attributes\":{}}"
StandardError.to_s == "StandardError"

However:

irb(main):001:0> require 'json'
=> true
irb(main):002:0> StandardError.to_json
=> "\"StandardError\""
irb(main):003:0> StandardError.to_s
=> "StandardError"

Comments and changes to this ticket

  • Prem Sichanugrist (sikachu)

    Prem Sichanugrist (sikachu) October 17th, 2010 @ 05:09 PM

    • State changed from “new” to “wontfix”
    • Importance changed from “” to “Low”

    Hi,

    From your report, I think you are suggesting that Rails' to_json should have the same outcome as pure irb, right?

    I can confirm the above outcome on Rails 3.0.1. However, I don't think this is a bug, as this is the way Rails make sure that everything can be converted to JSON. Calling to_json on an object would actually calling to_hash or returning instance_values if it doesn't implements to_hash. Unintentionally, StandardError doesn't implements to_hash, and so you got the instance variables (or class variables, in this case) instead.

  • Prem Sichanugrist (sikachu)

    Prem Sichanugrist (sikachu) October 17th, 2010 @ 05:11 PM

    One more thing,

    and you saw inheritable_attributes in the list of attributes because that's actually a hash storing the list of class's inheritable attributes.

  • Greg Hazel

    Greg Hazel October 18th, 2010 @ 12:16 AM

    Why should Rails to_json work any differently than regular to_json for non-Rails objects?

    Also, as you can see from Marshal, there is no need to dump anything about inheritable_attributes in order to instantiate StandardError again.

    >> Marshal.dump(StandardError)
    => "\004\bc\022StandardError"
    >> Marshal.load(Marshal.dump(StandardError))
    => StandardError
    

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages