mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* doc/syntax/methods.rdoc (Array/Hash Argument): Moved above Keyword
Arguments * doc/syntax/methods.rdoc (Keyword Arguments): Described ** for gathering arbitrary keyword arguments. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38817 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c3319d991a
commit
eeee190882
2 changed files with 38 additions and 17 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
Tue Jan 15 09:10:29 2013 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
|
* doc/syntax/methods.rdoc (Array/Hash Argument): Moved above Keyword
|
||||||
|
Arguments
|
||||||
|
* doc/syntax/methods.rdoc (Keyword Arguments): Described ** for
|
||||||
|
gathering arbitrary keyword arguments.
|
||||||
|
|
||||||
Tue Jan 15 08:56:37 2013 Eric Hodel <drbrain@segment7.net>
|
Tue Jan 15 08:56:37 2013 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
* doc/syntax/calling_methods.rdoc: Added document describing method
|
* doc/syntax/calling_methods.rdoc: Added document describing method
|
||||||
|
|
|
@ -153,25 +153,10 @@ This will raise a SyntaxError:
|
||||||
a + b + c
|
a + b + c
|
||||||
end
|
end
|
||||||
|
|
||||||
=== Keyword Arguments
|
|
||||||
|
|
||||||
Keyword arguments are similar to positional arguments with default values:
|
|
||||||
|
|
||||||
def add_values(first: 1, second: 2)
|
|
||||||
first + second
|
|
||||||
end
|
|
||||||
|
|
||||||
When calling a method with keyword arguments the arguments may appear in any
|
|
||||||
order. If an unknown keyword argument is sent by the caller an ArgumentError
|
|
||||||
is raised.
|
|
||||||
|
|
||||||
When mixing keyword arguments and positional arguments, all positional
|
|
||||||
arguments must appear before any keyword arguments.
|
|
||||||
|
|
||||||
=== Array/Hash Argument
|
=== Array/Hash Argument
|
||||||
|
|
||||||
Prefixing an argument with "*" causes any remaining arguments to be converted
|
Prefixing an argument with <code>*</code> causes any remaining arguments to be
|
||||||
to an Array:
|
converted to an Array:
|
||||||
|
|
||||||
def gather_arguments(*arguments)
|
def gather_arguments(*arguments)
|
||||||
p arguments
|
p arguments
|
||||||
|
@ -196,6 +181,35 @@ However, this only occurs if the method does not declare any keyword arguments.
|
||||||
gather_arguments_keyword 1, 2, three: 3
|
gather_arguments_keyword 1, 2, three: 3
|
||||||
#=> raises: unknown keyword: three (ArgumentError)
|
#=> raises: unknown keyword: three (ArgumentError)
|
||||||
|
|
||||||
|
Also, note that a bare <code>*</code> can be used to ignore arguments:
|
||||||
|
|
||||||
|
def ignore_arguments(*)
|
||||||
|
end
|
||||||
|
|
||||||
|
=== Keyword Arguments
|
||||||
|
|
||||||
|
Keyword arguments are similar to positional arguments with default values:
|
||||||
|
|
||||||
|
def add_values(first: 1, second: 2)
|
||||||
|
first + second
|
||||||
|
end
|
||||||
|
|
||||||
|
Arbitrary keyword arguments will be accepted with <code>**</code>:
|
||||||
|
|
||||||
|
def gather_arguments(first: nil, **rest)
|
||||||
|
p first, rest
|
||||||
|
end
|
||||||
|
|
||||||
|
gather_arguments first: 1, second: 2, third: 3
|
||||||
|
# prints 1 then {:second=>2, :third=>3}
|
||||||
|
|
||||||
|
When calling a method with keyword arguments the arguments may appear in any
|
||||||
|
order. If an unknown keyword argument is sent by the caller an ArgumentError
|
||||||
|
is raised.
|
||||||
|
|
||||||
|
When mixing keyword arguments and positional arguments, all positional
|
||||||
|
arguments must appear before any keyword arguments.
|
||||||
|
|
||||||
== Exception Handling
|
== Exception Handling
|
||||||
|
|
||||||
Methods have an implied exception handling block so you do not need to use
|
Methods have an implied exception handling block so you do not need to use
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue