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

Positional Args break when using Hashes #11

Open
kornypoet opened this issue Sep 25, 2013 · 0 comments
Open

Positional Args break when using Hashes #11

kornypoet opened this issue Sep 25, 2013 · 0 comments

Comments

@kornypoet
Copy link
Contributor

Positional arguments break when the last argument is a Hash:

# This does not work as expected
class Foo
  include Gorillib::Model

  field :name, String, position: 0
  field :info, Hash, position: 1
end

f = Foo.new('joebob', { boring: true })
f.name
#=> "joebob"
f.info
#=> nil

This can be solved by either not having a Hash as the last positional argument, or by passing in the (apparently not optional) options hash whenever using the #new method with positional args. Neither is a great alternative:

# Never put Hashes last in positional argument lists
class Foo
  include Gorillib::Model

  field :name, String, position: 1
  field :info, Hash, position: 0 # Reorganize the positions to avoid problems
end

f = Foo.new({ boring: true }, 'joebob')
f.name
#=> "joebob"
f.info
#=> {:boring=>true}

# Or alternatively

class Foo
  include Gorillib::Model

  field :name, String, position: 0
  field :info, Hash, position: 1
end

f = Foo.new('joebob', { boring: true }, {}) # Always remember this empty options hash
f.name
#=> "joebob"
f.info
#=> {:boring=>true}

The culprit is the #extract_options! method here.

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

No branches or pull requests

1 participant