Project

General

Profile

Actions

Feature #12593

closed

Allow compound assignements to work when destructuring arrays

Added by najamelan (Naja Melan) almost 8 years ago. Updated over 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:76395]

Description

a = [ 'a', 'b' ]
b = [ 'c', 'd' ]

def c

    return [ 'A', 'B' ], [ 'C', 'D' ]

end

a, b += c # -> would be awesome, but gives syntax error

a, b = a + c.first, b + c.last # clunky and will call method twice...

# current realistic use:
t, tt = c
a += t
b += tt

# desired result
#
p a == [ 'a', 'c', 'A', 'B' ] #-> true
p b == [ 'b', 'd', 'C', 'D' ] #-> true

I would propose that as

a, b = [ c, d ] # is equivalent to:
a = c 
b = d

a, b += [ c, d ] # would be equivalent to:
a += c
b += d

This not working surprised me. It could work with all compound assignment operators I think. Maybe even with some other operators.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0