mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* doc/syntax/methods.rdoc: Added Array Decomposition.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38872 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7a9bbf31fd
commit
1d5a5c235f
2 changed files with 65 additions and 0 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Fri Jan 18 14:11:01 2013 Eric Hodel <drbrain@segment7.net>
|
||||||
|
|
||||||
|
* doc/syntax/methods.rdoc: Added Array Decomposition.
|
||||||
|
|
||||||
Fri Jan 18 12:54:21 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Fri Jan 18 12:54:21 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* tool/rbinstall.rb (gem): Gem.ensure_gem_subdirectories makes
|
* tool/rbinstall.rb (gem): Gem.ensure_gem_subdirectories makes
|
||||||
|
|
|
@ -237,6 +237,67 @@ This will raise a SyntaxError:
|
||||||
a + b + c
|
a + b + c
|
||||||
end
|
end
|
||||||
|
|
||||||
|
=== Array Decomposition
|
||||||
|
|
||||||
|
You can decompose (unpack or extract values from) an Array using extra
|
||||||
|
parentheses in the arguments:
|
||||||
|
|
||||||
|
def my_method((a, b))
|
||||||
|
p a: a, b: b
|
||||||
|
end
|
||||||
|
|
||||||
|
my_method([1, 2])
|
||||||
|
|
||||||
|
This prints:
|
||||||
|
|
||||||
|
{:a=>1, :b=>2}
|
||||||
|
|
||||||
|
If the argument has extra elements in the Array they will be ignored:
|
||||||
|
|
||||||
|
def my_method((a, b))
|
||||||
|
p a: a, b: b
|
||||||
|
end
|
||||||
|
|
||||||
|
my_method([1, 2, 3])
|
||||||
|
|
||||||
|
This has the same output as above.
|
||||||
|
|
||||||
|
You can use a <code>*</code> to collect the remaining arguments. This splits
|
||||||
|
an Array into a first element and the rest:
|
||||||
|
|
||||||
|
def my_method((a, *b))
|
||||||
|
p a: a, b: b
|
||||||
|
end
|
||||||
|
|
||||||
|
my_method([1, 2, 3])
|
||||||
|
|
||||||
|
This prints:
|
||||||
|
|
||||||
|
{:a=>1, :b=>[2, 3]}
|
||||||
|
|
||||||
|
The argument will be decomposed if it responds to #to_ary. You should only
|
||||||
|
define #to_ary if you can use your object in place of an Array.
|
||||||
|
|
||||||
|
Use of the inner parentheses only uses one of the sent arguments. If the
|
||||||
|
argument is not an Array it will be assigned to the first argument in the
|
||||||
|
decomposition and the remaining arguments in the decomposition will be +nil+:
|
||||||
|
|
||||||
|
def my_method(a, (b, c), d)
|
||||||
|
p a: a, b: b, c: c, d: d
|
||||||
|
end
|
||||||
|
|
||||||
|
my_method(1, 2, 3)
|
||||||
|
|
||||||
|
This prints:
|
||||||
|
|
||||||
|
{:a=>1, :b=>2, :c=>nil, :d=>3}
|
||||||
|
|
||||||
|
You can nest decomposition arbitrarily:
|
||||||
|
|
||||||
|
def my_method(((a, b), c))
|
||||||
|
# ...
|
||||||
|
end
|
||||||
|
|
||||||
=== Array/Hash Argument
|
=== Array/Hash Argument
|
||||||
|
|
||||||
Prefixing an argument with <code>*</code> causes any remaining arguments to be
|
Prefixing an argument with <code>*</code> causes any remaining arguments to be
|
||||||
|
|
Loading…
Reference in a new issue