Project

General

Profile

Actions

Misc #15514

open

Add documentation for implicit array decomposition

Added by sos4nt (Stefan Schüßler) almost 6 years ago. Updated almost 6 years ago.

Status:
Open
Assignee:
-
[ruby-core:90913]

Description

The documentation for Array Decomposition says: "[...] you can decompose an Array during assignment using parenthesis [sic]" and gives an example:

(a, b) = [1, 2]

p a: a, b: b # prints {:a=>1, :b=>2}

But – as we all know – it's also possible without parentheses, i.e.

a, b = [1, 2]

p a: a , b: b #=> {:a=>1, :b=>2}

This also applies to block arguments when yielding multiple values vs. yielding a single array:

def foo
  yield 1, 2
end

def bar
  yield [1, 2]
end

foo { |a, b| p a: a, b: b }
#=> {:a=>1, :b=>2}

bar { |a, b| p a: a, b: b }
#=> {:a=>1, :b=>2}

In both cases, parentheses are optional.

This implicit array decomposition could be quite surprising for newcomers. The documentation should cover it.

Actions

Also available in: Atom PDF

Like0
Like0Like0