Splat Operator Notes Day 1 Flashcards
1
Q
how do you use splat to accept additional arguments?
A
def method(arg_1, arg_2, *other_args)
p arg_1 # “a”
p arg_2 # “b”
p other_args # [“c”, “d”, “e”]
2
Q
how do you use splat to decompose an array?
A
arr_1 = ["a", "b"] arr_2 = ["d", "e"] arr_3 = [ *arr_1, "c", *arr_2 ] < ["a", "b", "c", "d", "e"]
3
Q
how do you use splat to decompose a hash?
A
Double splat will only work with hashes where the keys are symbols:
old_hash = { a: 1, b: 2 }
new_hash = { **old_hash, c: 3 }
p new_hash # => {:a=>1, :b=>2, :c=>3}