Skip to content

Class methods (dump/load) rely on state #55

@troessner

Description

@troessner

I use JSON.dump and JSON.load a lot in my application. 99% of the time it's working as expected, but in a cucumber feature I get:

undefined method merge' for #JSON::Ext::Generator::State:0xb63993e0 (NoMethodError)

~/.bundler/ruby/1.8/my_lib-54ae46e6989b/lib/my_lib/event_type.rb:34:in store!'

~/.bundler/ruby/1.8/my_lib-54ae46e6989b/lib/my_lib/event_type.rb:23:in create'

./app/controllers/tenant/event_types_controller.rb:16:in create'

./features/step_definitions/web_steps.rb:29

./features/step_definitions/web_steps.rb:14:in with_scope'

./features/step_definitions/web_steps.rb:28:in /^(?:|I )press "([^"])"(?: within "([^"])")?$/'
features/event_types.feature:34:in And I press "Create"

This is what "store!" looks like:

def store!

$redis.set(@redis_key, JSON.dump(@Constraints))

end

This method is called a lot in my application, and I never had any problems with json dumping, except for in my cucumber feature.

The funny thing now is, that there is nothing wrong with "@Constraints" - in some tests before "@Constraints" hash value is the very same as in my failing cucumber spec and looks something like that (yes, no symbols, that's intentional):

{ 'required' => %w!item_id checkout_type!,

'type' => { 'item_id' => 'Integer' } }

To make this point clearer:

I debug my features with ruby-debug -> I have a working feature which calls "store!" -> I check the hash value of "@Constraints" -> let the feature successfully pass -> I step to my next (failing) feature -> I check the hash value of "@Constraints" -> it's the same as before -> I let the feature pass -> it fails with the error from above.

I don't know what the exact problem here is, but apparently JSON.dump / load does rely on some kind of state (what the error message hints at as well).

Not sure what's going wrong here, but as far as I can see, this is some kind of bug - a class method that may be called in any possible context (especially something like "dump()" / "load()") shouldn't rely on any kind of state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions