https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17097754782013-07-12T22:20:45ZRuby Issue Tracking SystemRuby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=404742013-07-12T22:20:45Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>This would avoid the need of hacks like this one: <a href="https://github.com/wycats/merb/blob/master/merb-action-args/lib/merb-action-args/mri_args.rb" class="external">https://github.com/wycats/merb/blob/master/merb-action-args/lib/merb-action-args/mri_args.rb</a></p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=404752013-07-12T22:23:15Zyorickpeterse (Yorick Peterse)yorickpeterse@gmail.com
<ul></ul><p>How would this take dynamic default values into account such as the<br>
following?:</p>
<pre><code> def some_method(time = Time.now)
end
</code></pre>
<p>Unless UnboundMethod#parameters would re-query this information on every<br>
call I don't see how it would be able to handle this.</p>
<p>Yorick</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=404762013-07-12T22:41:40Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>I could see this being valid if it were limited to showing literal types when the default value is a literal and something like :expression when it's anything more complex (though that obviously isn't the right choice since :expression as a default value is a perfectly-value literal.</p>
<p>For non-literals (and by literals I mean values that could safely be evaluated outside the context of that method... strings, symbols, floats, fixnums, bignums, true/false/nil) there's no real way to determine the type since it will depend on a method call, a constant lookup, or in the case of arrays and hashes, the elements contained within.</p>
<p>It would also not be possible to know whether the default value could safely be evaluated if it contained <em>any</em> constant lookups or method calls, since they may require being within the activation of that method (scoping, self methods, etc).</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=404772013-07-12T23:23:13Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Yorick, I believe I used exactly your case in my first example, didn't I? In that case, Method#parameters wouldn't evaluate Time.now but return a proc instead. It's up to the framework to decide if it will call the proc or not...</p>
<p>Charles, I understand your concerns, but if the framework explains how the default arguments are handled I think we should be fine.</p>
<p>For example, suppose the web framework states in the controller's documentation that this is how it performs the binding:</p>
<p>"""<br>
be aware that any default expression in the argument defaults will be evaluated twice. The framework will evaluate it once to determine how it should perform the binding and it will evaluate it once again when the method is called. Keep that in mind when deciding which expressions to use in default values. In case the default proc raises an exception when called by the web framework, no special binding rule will be set for that param. For instance:</p>
<p>def an_action(a=1, b=a+1)<br>
end</p>
<p>In that case, the param "a" is guaranteed to be bound to an Integer while no binding will occur for param "b".<br>
"""</p>
<p>Do you see? It's up to the frameworks making use of the arguments default values to decide upon calling the proc or not.</p>
<p>I'm interested in hearing any other considerations from you.</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=404782013-07-12T23:53:12Zyorickpeterse (Yorick Peterse)yorickpeterse@gmail.com
<ul></ul><p>Hm, seems I missed that part. Having said that, I think Charles makes a<br>
valid point in just returning something like <code>:expression</code> instead.<br>
There would probably be overhead to storing the actual values (be it as<br>
a Proc or something else).</p>
<p>Yorick</p>
<p>p.s. Note that I fully support this feature request since it would be<br>
neat to have this information (or at least a part of it) available.</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=404792013-07-13T00:02:37Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>I'm not sure if returning a proc instead of something else would be really an overhead... And the fact that it is a proc is enough for the application using Method#parameters to ignore it if it doesn't want to perform any additional checks...</p>
<p>I just find that returning a proc instead of something else would be generally more useful and allow code to do smarter things with it...</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=407152013-07-27T16:42:25Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>There can be a default value expression that refers external scope, e.g.</p>
<p>def foo(a, b=a+2)<br>
end</p>
<p>In the case like this, there's no way to define a proc to return the default value.<br>
So your proposal is theoretically impossible in general, except for some cases (say literals),<br>
and I don't want to introduce special cases here.</p>
<p>Matz.</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=407162013-07-27T21:07:34Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>I don't understand, I've expressed already my opinion on how it should behave in the case you pointed out directly in the description. In such cases you could return a proc as ->{raise UnstranslatableExpression} or something like that and it would be fine to me.</p>
<p>Could you please reconsider?</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=407442013-07-29T10:18:37Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>The option expression can be very complex. IMHO covering simplest cases does not help.<br>
Besides that, providing proc would introduce huge complexity. I don't think proposed incomplete proc behavior is not worth complexity.</p>
<p>Matz.</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=407502013-07-29T21:50:03Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Ok, thanks for considering. I'll try to think in other approaches to deal with params bindings to propose to the Rails web framework, but that won't be as elegant as having the binding rules extracted directly from the default values (since in those cases the default values wouldn't be a proc if the users read the documentation).</p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=407512013-07-29T23:23:16Zmatsuda (Akira Matsuda)ronnie@dio.jp
<ul></ul><p>Rodrigo,</p>
<p>Here's my implementation of "params bindings to propose to the Rails web<br>
framework", which might interest you.<br>
<a href="https://github.com/asakusarb/action_args/blob/master/lib/action_args/params_handler.rb" class="external">https://github.com/asakusarb/action_args/blob/master/lib/action_args/params_handler.rb</a><br>
Also, here's the craziest example of "other approaches", which might just<br>
entertain you (I mean, you might not want to use it in your actual code).<br>
<a href="https://speakerdeck.com/a_matsuda/ruby-2-dot-0-on-rails-in-production?slide=64" class="external">https://speakerdeck.com/a_matsuda/ruby-2-dot-0-on-rails-in-production?slide=64</a><br>
HTH</p>
<p>On Mon, Jul 29, 2013 at 9:50 PM, rosenfeld (Rodrigo Rosenfeld Rosas) <<br>
<a href="mailto:rr.rosas@gmail.com" class="email">rr.rosas@gmail.com</a>> wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Method#parameters should include the default value (Rejected)" href="https://bugs.ruby-lang.org/issues/8629">#8629</a> has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).</p>
<h2>Ok, thanks for considering. I'll try to think in other approaches to deal<br>
with params bindings to propose to the Rails web framework, but that won't<br>
be as elegant as having the binding rules extracted directly from the<br>
default values (since in those cases the default values wouldn't be a proc<br>
if the users read the documentation).</h2>
<p>Feature <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Method#parameters should include the default value (Rejected)" href="https://bugs.ruby-lang.org/issues/8629">#8629</a>: Method#parameters should include the default value<br>
<a href="https://bugs.ruby-lang.org/issues/8629#change-40750" class="external">https://bugs.ruby-lang.org/issues/8629#change-40750</a></p>
<p>Author: rosenfeld (Rodrigo Rosenfeld Rosas)<br>
Status: Rejected<br>
Priority: Normal<br>
Assignee: matz (Yukihiro Matsumoto)<br>
Category: core<br>
Target version:</p>
<p>def a_method arg1=1, now = Time.now, arg2 = arg1 + 1<br>
end</p>
<p>method(:a_method).parameters == [[:opt, :arg1], [:opt, :now], [:opt,<br>
:arg2]]</p>
<p>I'd prefer if it could return [[:opt, :arg1, 1], [:opt, :now, now_proc],<br>
[:opt, :arg2, arg2_proc]], and expect now_proc[] to be the current time and<br>
arg2_proc[] to raise an exception since arg1 is not defined.</p>
<p>Rationale:</p>
<p>Ruby doesn't support optional typing such as:</p>
<p>def a_method Date date, Integer n = 0, String name = ''<br>
end</p>
<p>Groovy does, and this allows Grails to perform some interesting stuff,<br>
like params binding in controller methods.</p>
<p>If Ruby allowed the default values to be introspected, web frameworks<br>
would be able to achieve a similar binding feature. For example, they could<br>
use the default to decide upon how to bind the param. They could use the<br>
default_value.class or if the default value is nil it could be specified by<br>
providing the class itself. For instance:</p>
<p>def an_action name: '', parent_name: String, age: Integer, date: Date.today<br>
end</p>
<p>Of course, you'd need to set up the framework so that it knows how you<br>
intend parse dates and other special types from string, but this could make<br>
the developer life easier and safer against this kind of attack (like<br>
trying to instantiate a hash, etc).</p>
<p>An alternative would be something like:</p>
<p>def an_action params = {name: :string, age: :integer, date: :date}<br>
end</p>
<p>You get the idea. Many APIs would be possible to be built if we're able to<br>
easily get access to the default values when inspecting a method.</p>
<p>Could you please consider this idea?</p>
<p>--<br>
<a href="http://bugs.ruby-lang.org/" class="external">http://bugs.ruby-lang.org/</a></p>
</blockquote>
<p>--<br>
Akira Matsuda<a href="mailto:ronnie@dio.jp" class="email">ronnie@dio.jp</a></p> Ruby master - Feature #8629: Method#parameters should include the default valuehttps://bugs.ruby-lang.org/issues/8629?journal_id=407522013-07-29T23:53:18Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Hi Akira, thanks for your valuable input.</p>
<p>While your gem action_args is indeed interesting, it's not quite what<br>
I'm looking for since it will only bind the params to argument variables<br>
but won't perform any type conversion from string, which is what I'm<br>
mostly interested in.</p>
<p>With regards to your slide, I've seen some crazy implementations in the<br>
past (as used by Merb) to try to get the default values or the arguments<br>
as well. I mean, it's definitely easier with Ruby 2.0.0, but still looks<br>
like a bit of Voodoo. That's why I created this ticket so that we<br>
wouldn't need those complicated implementation attempts just to get<br>
access to the default values for the arguments.</p>
<p>It would be much easier if we had an easier way to get access to the<br>
default values (using both the new hash-like syntax or the old one) even<br>
if it's not possible to get the default value in some cases...</p>
<p>Cheers,<br>
Rodrigo.</p>
<p>Em 29-07-2013 11:21, Akira Matsuda escreveu:</p>
<blockquote>
<p>Rodrigo,</p>
<p>Here's my implementation of "params bindings to propose to the Rails<br>
web framework", which might interest you.<br>
<a href="https://github.com/asakusarb/action_args/blob/master/lib/action_args/params_handler.rb" class="external">https://github.com/asakusarb/action_args/blob/master/lib/action_args/params_handler.rb</a><br>
Also, here's the craziest example of "other approaches", which might<br>
just entertain you (I mean, you might not want to use it in your<br>
actual code).<br>
<a href="https://speakerdeck.com/a_matsuda/ruby-2-dot-0-on-rails-in-production?slide=64" class="external">https://speakerdeck.com/a_matsuda/ruby-2-dot-0-on-rails-in-production?slide=64</a><br>
HTH</p>
<p>On Mon, Jul 29, 2013 at 9:50 PM, rosenfeld (Rodrigo Rosenfeld Rosas)<br>
<<a href="mailto:rr.rosas@gmail.com" class="email">rr.rosas@gmail.com</a> <a href="mailto:rr.rosas@gmail.com" class="email">mailto:rr.rosas@gmail.com</a>> wrote:</p>
<pre><code>Issue #8629 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).
Ok, thanks for considering. I'll try to think in other approaches
to deal with params bindings to propose to the Rails web
framework, but that won't be as elegant as having the binding
rules extracted directly from the default values (since in those
cases the default values wouldn't be a proc if the users read the
documentation).
----------------------------------------
Feature #8629: Method#parameters should include the default value
https://bugs.ruby-lang.org/issues/8629#change-40750
Author: rosenfeld (Rodrigo Rosenfeld Rosas)
Status: Rejected
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version:
def a_method arg1=1, now = Time.now, arg2 = arg1 + 1
end
method(:a_method).parameters == [[:opt, :arg1], [:opt, :now],
[:opt, :arg2]]
I'd prefer if it could return [[:opt, :arg1, 1], [:opt, :now,
now_proc], [:opt, :arg2, arg2_proc]], and expect now_proc[] to be
the current time and arg2_proc[] to raise an exception since arg1
is not defined.
Rationale:
Ruby doesn't support optional typing such as:
def a_method Date date, Integer n = 0, String name = ''
end
Groovy does, and this allows Grails to perform some interesting
stuff, like params binding in controller methods.
If Ruby allowed the default values to be introspected, web
frameworks would be able to achieve a similar binding feature. For
example, they could use the default to decide upon how to bind the
param. They could use the default_value.class or if the default
value is nil it could be specified by providing the class itself.
For instance:
def an_action name: '', parent_name: String, age: Integer, date:
Date.today
end
Of course, you'd need to set up the framework so that it knows how
you intend parse dates and other special types from string, but
this could make the developer life easier and safer against this
kind of attack (like trying to instantiate a hash, etc).
An alternative would be something like:
def an_action params = {name: :string, age: :integer, date: :date}
end
You get the idea. Many APIs would be possible to be built if we're
able to easily get access to the default values when inspecting a
method.
Could you please consider this idea?
--
http://bugs.ruby-lang.org/
</code></pre>
<p>--<br>
Akira Matsuda<<a href="mailto:ronnie@dio.jp" class="email">ronnie@dio.jp</a> <a href="mailto:ronnie@dio.jp" class="email">mailto:ronnie@dio.jp</a>></p>
</blockquote>