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

With bundler, an OpenStruct can't have a field called 'gem' (and it's really hard to troubleshoot -- redefine_method) #27

Open
ianfixes opened this issue May 21, 2021 · 1 comment

Comments

@ianfixes
Copy link

After many hours of head-scratching, I can offer this bug report:

OpenStruct.new(gem: 'foobar').gem

The above code works in "bare" ruby, but not with bundler.

Steps to reproduce:

$ mkdir ostruct-bug

$ cd ostruct-bug

$ echo "source 'https://rubygems.org'" > Gemfile

$ bundle install
The Gemfile specifies no dependencies
Resolving dependencies...
Bundle complete! 0 Gemfile dependencies, 1 gem now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]

$ bundle -v
Bundler version 2.1.4

$ ruby -e "require 'ostruct' ; puts OpenStruct.new(gem: 'foobar').gem"
foobar

$ bundle exec ruby -e "require 'ostruct' ; puts OpenStruct.new(gem: 'foobar').gem"
Traceback (most recent call last):
	1: from -e:1:in `<main>'
/Users/ikatz/.rbenv/versions/2.7.2/lib/ruby/2.7.0/bundler/rubygems_integration.rb:316:in `block (2 levels) in replace_gem': wrong number of arguments (given 0, expected 1+) (ArgumentError)

$ bundle exec ruby -e "require 'ostruct' ; puts OpenStruct.new(germ: 'foobar').germ"
foobar

Whether or not this is a problem with bundler (vs ostruct), the error is very unhelpful -- gem seems to all appearances like a perfectly valid attribute name and jumping into bundler source is generally not my first thought when debugging my own project.

The relevant bundler code is here:
https://github.com/rubygems/bundler/blob/master/lib/bundler/rubygems_integration.rb#L316

It would seem that the correct behavior in ostruct would be to check whether a method name is available (possibly with self.respond_to?(new_attribute_name) at the time the attribute is being defined , so that it can produce a warning or exception immediately instead of cryptic behavior when the attribute is later accessed.

@olleolleolle
Copy link
Contributor

👋 @deivid-rodriguez Hope this report finds you well!

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

No branches or pull requests

2 participants