Zoeken…


Overzicht

De meeste magie van destructurering maakt gebruik van de operator splat ( * ).

Voorbeeld Resultaat / opmerking
a, b = [0,1] a=0, b=1
a, *rest = [0,1,2,3] a=0, rest=[1,2,3]
a, * = [0,1,2,3] a=0 Gelijk aan .first
*, z = [0,1,2,3] z=3 Gelijk aan .last

Blokstructuren vernietigen

triples = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

triples.each { |(first, second, third)| puts second }
# 2
# 5
# 8

triples.map { |(first, *rest)| rest.join(' ') } # => ["2 3", "5 6", "8 9"]


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow