Skip to content

Instantly share code, notes, and snippets.

@hyfather
Created October 24, 2011 13:49
Show Gist options
  • Save hyfather/1309067 to your computer and use it in GitHub Desktop.
Save hyfather/1309067 to your computer and use it in GitHub Desktop.
Demonstrates how the splat operator works in Ruby.
lambda{|*args| args}.call(1, 2)
#=> [1, 2]
# This is the splat operator used in the 'collecting mode'.
# The splat before the args ensured that all arguments were marshalled into an Array.
[1, 2, [3, 4]]
#=> [1, 2, [3, 4]]
[1, 2, *[3, 4]]
#=> [1, 2, 3, 4]
# This is the unmarshalling 'mode' of the splat operator.
# Notice how [3, 4] was 'unwrapped' into only the list of elements inside.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment