From: Rodrigo Rosenfeld Rosas Date: 2013-07-29T23:46:51+09:00 Subject: [ruby-core:56260] Re: [ruby-trunk - Feature #8629] Method#parameters should include the default value This is a multi-part message in MIME format. --------------090409020406070600020409 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi Akira, thanks for your valuable input. While your gem action_args is indeed interesting, it's not quite what I'm looking for since it will only bind the params to argument variables but won't perform any type conversion from string, which is what I'm mostly interested in. With regards to your slide, I've seen some crazy implementations in the past (as used by Merb) to try to get the default values or the arguments as well. I mean, it's definitely easier with Ruby 2.0.0, but still looks like a bit of Voodoo. That's why I created this ticket so that we wouldn't need those complicated implementation attempts just to get access to the default values for the arguments. It would be much easier if we had an easier way to get access to the default values (using both the new hash-like syntax or the old one) even if it's not possible to get the default value in some cases... Cheers, Rodrigo. Em 29-07-2013 11:21, Akira Matsuda escreveu: > Rodrigo, > > Here's my implementation of "params bindings to propose to the Rails > web framework", which might interest you. > https://github.com/asakusarb/action_args/blob/master/lib/action_args/params_handler.rb > Also, here's the craziest example of "other approaches", which might > just entertain you (I mean, you might not want to use it in your > actual code). > https://speakerdeck.com/a_matsuda/ruby-2-dot-0-on-rails-in-production?slide=64 > HTH > > > On Mon, Jul 29, 2013 at 9:50 PM, rosenfeld (Rodrigo Rosenfeld Rosas) > > wrote: > > > 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/ > > > > > -- > Akira Matsuda> --------------090409020406070600020409 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
Hi Akira, thanks for your valuable input.

While your gem action_args is indeed interesting, it's not quite what I'm looking for since it will only bind the params to argument variables but won't perform any type conversion from string, which is what I'm mostly interested in.

With regards to your slide, I've seen some crazy implementations in the past (as used by Merb) to try to get the default values or the arguments as well. I mean, it's definitely easier with Ruby 2.0.0, but still looks like a bit of Voodoo. That's why I created this ticket so that we wouldn't need those complicated implementation attempts just to get access to the default values for the arguments.

It would be much easier if we had an easier way to get access to the default values (using both the new hash-like syntax or the old one) even if it's not possible to get the default value in some cases...

Cheers,
Rodrigo.

Em 29-07-2013 11:21, Akira Matsuda escreveu:
Rodrigo,

Here's my implementation of "params bindings to propose to the Rails web framework", which might interest you. https://github.com/asakusarb/action_args/blob/master/lib/action_args/params_handler.rb
Also, here's the craziest example of "other approaches", which might just entertain you (I mean, you might not want to use it in your actual code). https://speakerdeck.com/a_matsuda/ruby-2-dot-0-on-rails-in-production?slide=64
HTH


On Mon, Jul 29, 2013 at 9:50 PM, rosenfeld (Rodrigo Rosenfeld Rosas) <rr.rosas@gmail.com> wrote:

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/




--
Akira Matsuda<ronnie@dio.jp>

--------------090409020406070600020409--