Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If key has not been defined, Hashie::Mash should raise NoMethodError instead of returning nil #17

Closed
wants to merge 2 commits into from

Conversation

carltonbrown
Copy link

I've found this class to be very useful, but one thing I don't like is that an undefined key does not cause an exception to be raised. I can't tell the difference between a key that's been deliberately set to nil from a key that wasn't set at all, and this ambiguity makes it difficult to track down where errors were introduced (or not). Please consider pulling! Thanks.

@garrettux
Copy link

+1

@carltonbrown
Copy link
Author

I fixed 5/6 of the tests that broke as a result of this change, and checked them in... however I'm not sure what to do about the failing spec "should accept a default block". I don't understand the use case of initializing with a block, and I don't understand if it's really important that the default field be initialized to nil. Any thoughts?

@cmar
Copy link
Contributor

cmar commented Apr 20, 2011

I like the default behavior because I never know whats coming back from JSON that I'm parsing. Could you maybe add this behavior as a subclass of Mash?

@carltonbrown
Copy link
Author

Doesn't the default behavior make that problem worse though? You can't really know whether the nils came from your source data or Mash spoofed them for you.

@cmar
Copy link
Contributor

cmar commented Apr 20, 2011

The scenario is that I write code to display values that come back from an external web service. If that service occasionally provides an attribute, I can display it on my page without worrying about an exception. I've run into services that don't provide default nil values for undefined, they just don't provide that attribute.

I think your exception is a great idea in some cases. Thats why I was suggesting to put it in a subclass.

Don't forget you can always check if the key exists with .key?

@mbleigh
Copy link
Contributor

mbleigh commented Apr 25, 2011

Sorry, but I have to disagree here. A Mash is a Hash with method-based access to its keys. Therefore it should behave in the same manner a Hash would if you access a key that isn't initialized. And, in fact, if you strongly desire this functionality you can have it using the default setting block initializer:

hash = Hashie::Mash.new{ raise NoMethodError }
hash.abc # => NoMethodError
hash.abc = 1234
hash.abc # => 1234

As a result, this is not going to be merged. Thanks for the discussion though.

@mbleigh mbleigh closed this Apr 25, 2011
@carltonbrown
Copy link
Author

Fair enough. The default block will get it done for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants