Splat Operator Notes Day 1 Flashcards

You may prefer our related Brainscape-certified 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”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly