Skip to content

Instantly share code, notes, and snippets.

@jordelver
Created September 26, 2012 21:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordelver/3790808 to your computer and use it in GitHub Desktop.
Save jordelver/3790808 to your computer and use it in GitHub Desktop.
Destructuring assignment in Ruby and splats!
# 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
@nemuba
Copy link

nemuba commented Dec 18, 2019

Ruby is awesome !

@waqas02
Copy link

waqas02 commented Apr 27, 2021

way to do the same thing in hashes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment