Project

General

Profile

Actions

Feature #4172

closed

Named arguments/parameters support

Added by rosenfeld (Rodrigo Rosenfeld Rosas) over 13 years ago. Updated about 12 years ago.

Status:
Rejected
Target version:
-
[ruby-core:33774]

Description

=begin
It is usual in the Ruby community to use the idiom

def method(options={})
param1, param2 = options.values_at(:param1, :param2) # or {param1: 'default1', param2: 2}.collect{|k, v| options[k] || v }
...
end

instead of

def method(param1, param2)
...
end

Perl 6 supports named arguments (or parameters) out-of-the-box. Suppose the following method (not valid Ruby actually):

def tag(tag_name, :content(''), :attributes({}), :closing_tag(true))
...
end

It would be great if this could be called as

hr = tag('hr', closing_tag -> false, attributes -> {style: 'color: blue'})

It think it should be even possible to call it like

tag(tag_name: 'div', content: 'Ruby is getting even more awesome with this feature!')

I think it is much more legible than

tag('hr', '', {}, false)

or than using an 'options' hash parameter.

Is there any reasons why Ruby doesn't support named parameters as part of the syntax?
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0