https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112016-03-02T12:53:27ZRuby Issue Tracking SystemRuby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=572432016-03-02T12:53:27Zniko (Niko Dittmann)
<ul></ul><p>It's this commit: <a href="https://github.com/ruby/ruby/blob/7fa21558051e5412dcb790f528e392476edd4389/lib/ostruct.rb" class="external">https://github.com/ruby/ruby/blob/7fa21558051e5412dcb790f528e392476edd4389/lib/ostruct.rb</a></p>
<p>By defining the getters and setters lazily the Kernel, Object and BasicObject instance methods shine through and #method_missing doesn't kick in. Therefor the #send semantics is broken for methods colliding with methods defined in parent classes.</p> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=572692016-03-04T00:45:01Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Indeed, latest optimization of <code>OpenStruct</code> now allows conflicts with Object private methods.</p>
<p>I didn't realize it, but conflicts with public methods are already ignored (i.e. <code>OpenStruct.new(hash: 'code').hash</code> does not return 'code)</p>
<p>Note that <code>OpenStruct.new(format: :bar).public_send :format</code> does return <code>:bar</code>.</p>
<p>Possibilities:</p>
<p>a) Keep behavior the same and rubyists can alleviate these by using <code>public_send</code> instead of <code>send</code></p>
<p>b) Modify <code>new</code> to check for conflict between keys and <code>Object</code> private instance methods and define actual methods in these cases.</p>
<p>c) Revert optimization. Optionally create <code>OpenStruct.lazy</code> for the optimized version.</p>
<p>I'm in favor for the later, but maybe I'm missing alternatives?</p>
<p>BTW, I thought at first that we could undefine private instance methods of <code>OpenStruct</code>, except for the usual callbacks and modify <code>respond_to_missing?</code> + <code>method_missing</code> so that calls to these private methods still work.</p>
<p>Sadly, there's no way to know from <code>method_missing</code> if that method is called privately or publicly, so this would effectively make all private methods become public which is not acceptable.</p> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=572792016-03-04T09:57:35ZEregon (Benoit Daloze)
<ul></ul><p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>Sadly, there's no way to know from <code>method_missing</code> if that method is called privately or publicly, so this would effectively make all private methods become public which is not acceptable.</p>
</blockquote>
<p>There is a way now, mentioned in <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Global method inside Delegator causes NameError (Open)" href="https://redmine.ruby-lang.org/issues/12113">#12113</a>. I'm not sure whether it is good idea to use it, though.</p> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=579632016-04-06T17:13:27Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/12251">Bug #12251</a>: DelegateClass(OpenStruct) behavior in 2.3.0 different from 2.2</i> added</li></ul> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=579642016-04-06T18:26:27Zdblock (Daniel Doubrovkine)dblock@dblock.org
<ul></ul><blockquote>
<p>a) Keep behavior the same and rubyists can alleviate these by using <code>public_send</code> instead of <code>send</code></p>
</blockquote>
<p>It doesn't seem that swapping <code>send</code> by <code>public_send</code> has any effect, at least not in the example in <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: DelegateClass(OpenStruct) behavior in 2.3.0 different from 2.2 (Closed)" href="https://redmine.ruby-lang.org/issues/12251">#12251</a>.</p>
<p>Is there a workaround for existing code that would make things work the way it worked in Ruby 2.2.x?</p> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=588962016-05-29T13:35:12Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Has duplicate</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/12349">Bug #12349</a>: Can't load OpenStruct with Syck with Ruby 2.3.x</i> added</li></ul> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=588982016-05-29T13:36:14Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Has duplicate</strong> deleted (<i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/12349">Bug #12349</a>: Can't load OpenStruct with Syck with Ruby 2.3.x</i>)</li></ul> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=823282019-10-25T20:08:49Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/15409">Bug #15409</a>: OpenStruct error when attribute is called 'method'</i> added</li></ul> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=876322020-09-22T20:08:34Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul> Ruby master - Bug #12136: OpenStruct.new(format: :bar).send :format # => too few argumentshttps://redmine.ruby-lang.org/issues/12136?journal_id=928272021-07-09T05:27:12Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-6 priority-4 priority-default closed" href="/issues/18032">Bug #18032</a>: Openstruct is ~20..25x slower with Ruby 3.0.0 and 3.0.1 compared to earlier versions</i> added</li></ul>