Issue #12136 has been updated by Marc-Andre Lafortune.


Indeed, latest optimization of `OpenStruct` now allows conflicts with Object private methods.

I didn't realize it, but conflicts with public methods are already ignored (i.e. `OpenStruct.new(hash: 'code').hash` does not return 'code)

Note that `OpenStruct.new(format: :bar).public_send :format` does return `:bar`.

Possibilities:

a) Keep behavior the same and rubyists can alleviate these by using `public_send` instead of `send`

b) Modify `new` to check for conflict between keys and `Object` private instance methods and define actual methods in these cases.

c) Revert optimization. Optionally create `OpenStruct.lazy` for the optimized version.

I'm in favor for the later, but maybe I'm missing alternatives?

BTW, I thought at first that we could undefine private instance methods of `OpenStruct`, except for the usual callbacks and modify `respond_to_missing?` + `method_missing` so that calls to these private methods still work.

Sadly, there's no way to know from `method_missing` if that method is called privately or publicly, so this would effectively make all private methods become public which is not acceptable.


----------------------------------------
Bug #12136: OpenStruct.new(format: :bar).send :format # => too few arguments
https://bugs.ruby-lang.org/issues/12136#change-57269

* Author: Niko Dittmann
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
#send(:format) to an OpenStruct with a field named :format raises an ArgumentError in Ruby 2.3.0:

~~~
OpenStruct.new(format: :bar).send :format
ArgumentError: too few arguments
~~~

It works as expected in ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux] and with any other method name I tried:

~~~
OpenStruct.new(f: :bar).send :f
=> :bar
~~~

String or Symbol in the OpenStruct definition and as argument of #send make no difference.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>