Comment on ES6 in Depth: DestructuringparentComments−bshimmin11yOther languages have this too, eg. Ruby: a, b = [1, 2] a, b = {:a => 1, :b => 2} Similarly in Python, at least for the array example (there's probably some way of destructuring a dictionary in Python, but I don't know it myself).PHP also has `list()`...−petepete11yFor arrays, yes, but that won't work for Ruby hashes (which aren't ordered): [1] pry ~ » a, b = {a: 1, b: 2} { :a => 1, :b => 2 } [2] pry ~ » a { :a => 1, :b => 2 } [3] pry ~ » b nil−bshimmin11yYou're quite right, I meant to add `values_at` in the hash example! And the syntax is still not so nice: irb(main):005:0> a, b = {:a => 1, :b => 2}.values_at(:a, :b) => [1, 2] Or: irb(main):006:0> a, b = (h = {:a => 1, :b => 2}).values_at(*h.keys) => [1, 2] Edit: Ruby's hashes are ordered since 1.9, incidentally.−petepete11ya, b = {a: 1, b: 2}.values
Comments
Other languages have this too, eg. Ruby:
Similarly in Python, at least for the array example (there's probably some way of destructuring a dictionary in Python, but I don't know it myself).PHP also has `list()`...
For arrays, yes, but that won't work for Ruby hashes (which aren't ordered):
You're quite right, I meant to add `values_at` in the hash example! And the syntax is still not so nice:
Or: Edit: Ruby's hashes are ordered since 1.9, incidentally.a, b = {a: 1, b: 2}.values