Skip to content

Instantly share code, notes, and snippets.

@shinokada
Created May 27, 2014 13:13
Show Gist options
  • Save shinokada/5cff7a0191dff897484e to your computer and use it in GitHub Desktop.
Save shinokada/5cff7a0191dff897484e to your computer and use it in GitHub Desktop.
# strange splat
[*0..9] # [1,2,3,4,5,6,7,8,9]
# http://endofline.wordpress.com/2011/01/21/the-strange-ruby-splat/
# splat creates an array
first, *list = [1,2,3,4] # first= 1, list= [2,3,4]
*list, last = [1,2,3,4] # list= [1,2,3], last= 4
first, *center, last = [1,2,3,4] # first= 1, center= [2,3], last=4
a = *"Hello" #=> ["Hello"]
"Hello".to_a #=> NoMethodError: undefined method `to_a' for "Hello":String
a = *(1..3) #=> [1, 2, 3]
a = *[1,2,3] #=> [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment