From eeee1908824cd0ebaf8ea6c4a2019be887b280ed Mon Sep 17 00:00:00 2001 From: drbrain Date: Tue, 15 Jan 2013 00:11:46 +0000 Subject: * 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 --- ChangeLog | 7 +++++++ doc/syntax/methods.rdoc | 48 +++++++++++++++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index a3c516e43c..47c6cd3aff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Jan 15 09:10:29 2013 Eric Hodel + + * 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 * doc/syntax/calling_methods.rdoc: Added document describing method diff --git a/doc/syntax/methods.rdoc b/doc/syntax/methods.rdoc index 832ba269b0..70a916387d 100644 --- a/doc/syntax/methods.rdoc +++ b/doc/syntax/methods.rdoc @@ -153,25 +153,10 @@ This will raise a SyntaxError: a + b + c 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 -Prefixing an argument with "*" causes any remaining arguments to be converted -to an Array: +Prefixing an argument with * causes any remaining arguments to be +converted to an Array: def gather_arguments(*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 #=> raises: unknown keyword: three (ArgumentError) +Also, note that a bare * 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 **: + + 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 Methods have an implied exception handling block so you do not need to use -- cgit v1.2.3