Created
September 26, 2012 21:45
-
-
Save jordelver/3790808 to your computer and use it in GitHub Desktop.
Destructuring assignment in Ruby and splats!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Destructuring assignment in Ruby | |
# http://po-ru.com/diary/destructuring-assignment-in-ruby/ | |
numbers = [ 1, 2, 3, 4, 5] | |
=> [1, 2, 3, 4, 5] | |
# Destructuring assignment | |
a, b, c = numbers | |
a | |
=> 1 | |
b | |
=> 2 | |
c | |
=> 3 | |
# Gather up array elements | |
a, *b, c = numbers | |
a | |
=> 1 | |
b | |
=> [2, 3, 4] | |
c | |
=> 5 | |
# Ignore elements you don't care about | |
a, *, b = numbers | |
a | |
=> 1 | |
b | |
=> 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ruby is awesome !