[#55853] ruby 1.9.3 p448 breaks ABI — V咜 Ondruch <v.ondruch@...>

Hi,

13 messages 2013/07/08

[#55951] [ruby-trunk - Bug #8625][Open] IO#read(len, buf) shortens buf even if data is not read actually — "no6v (Nobuhiro IMAI)" <nov@...>

10 messages 2013/07/11

[#55976] [ruby-trunk - Feature #8629][Open] Method#parameters should include the default value — "rosenfeld (Rodrigo Rosenfeld Rosas)" <rr.rosas@...>

13 messages 2013/07/12

[#55985] [ruby-trunk - Feature #8631][Open] Add a new method to ERB to allow assigning the local variables from a hash — "rosenfeld (Rodrigo Rosenfeld Rosas)" <rr.rosas@...>

19 messages 2013/07/12

[#56004] [ruby-trunk - Feature #8636][Open] Documentation hosting on ruby-lang.org — "zzak (Zachary Scott)" <e@...>

18 messages 2013/07/15

[#56019] [ruby-trunk - Feature #8639][Open] Add Queue#each — "avdi (Avdi Grimm)" <avdi@...>

15 messages 2013/07/15

[#56027] [CommonRuby - Feature #8640][Open] Add Time#elapsed to return nanoseconds since creation — "tenderlovemaking (Aaron Patterson)" <aaron@...>

24 messages 2013/07/15

[#56041] [CommonRuby - Feature #8643][Open] Add Binding.from_hash — "rosenfeld (Rodrigo Rosenfeld Rosas)" <rr.rosas@...>

26 messages 2013/07/16

[#56087] [ruby-trunk - Feature #8658][Open] Process.clock_gettime — "akr (Akira Tanaka)" <akr@...>

23 messages 2013/07/19

[#56096] [CommonRuby - Feature #8661][Open] Add option to print backstrace in reverse order(stack frames first & error last) — "gary4gar (Gaurish Sharma)" <gary4gar@...>

18 messages 2013/07/20

[#56193] [ruby-trunk - Bug #8693][Open] lambda invoked by yield acts as a proc with respect to return — "rits (First Last)" <redmine@...>

33 messages 2013/07/26

[#56274] [ruby-trunk - Bug #8709][Open] Dir.glob should return sorted file list — "tommorris (Tom Morris)" <tom@...>

19 messages 2013/07/30

[ruby-core:55980] [ruby-trunk - Feature #8629] Method#parameters should include the default value

From: "rosenfeld (Rodrigo Rosenfeld Rosas)" <rr.rosas@...>
Date: 2013-07-12 14:23:13 UTC
List: ruby-core #55980
Issue #8629 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


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...

Charles, I understand your concerns, but if the framework explains how the default arguments are handled I think we should be fine.

For example, suppose the web framework states in the controller's documentation that this is how it performs the binding:

"""
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 more 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:

def an_action(a=1, b=a+1)
end

In that case, the param "a" is guaranteed to be bound to an Integer while no binding will occur for param "b".
"""

Do you see? It's up to the frameworks making use of the arguments default values to decide upon calling the proc or not.

I'm interested in hearing any other considerations from you.
----------------------------------------
Feature #8629: Method#parameters should include the default value
https://bugs.ruby-lang.org/issues/8629#change-40477

Author: rosenfeld (Rodrigo Rosenfeld Rosas)
Status: Open
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/

In This Thread