-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplat_operator.rb
35 lines (28 loc) · 952 Bytes
/
splat_operator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Source: https://thoughtbot.com/blog/ruby-splat-operator
# Takeaways
# 1. `*` is the splat operator
# 2. It destructures and constructs arrays
# 3. Used for variable function arguments
# Destructuring arrays
puts "# Destructuring arrays"
array = [1,2,3]
first, rest, last = *array
puts "First of #{array} is #{first}"
puts "Rest of #{array} is #{rest}"
puts "Last of #{array} is #{last}"
# Constructing arrays
puts "\n# Constructing arrays"
array = *1,2,3
puts "Popping #{array} gives #{array.pop}"
# When does it construct? Defining a method!"
puts "\n# When does it construct? Defining a method!"
def variable_args(*args)
puts "args is #{args} of type #{args.class}"
end
variable_args(1,2,3)
# When does it destructure? Passing an array argument to a method!"
puts "\n# When does it destructure? Passing an array argument to a method!"
def deconstruct_args(x,y,z)
puts "x is #{x} of type #{x.class}"
end
deconstruct_args(*[1,2,3])