Methods
- Ruby functions start with
defjust like python, but end withendkeyword. - splat operator (
*) to destructure an array into a List. This is similar to Python splat operator
>>> x = [10, 20, 30]
>>> a, *b = x
>>> print(f'{a=}, {b=}')
a=10, b=[20, 30]
Python ☝️, Ruby 👇
>> first, *rest, last = ["a", "b", "c", "d"]
=> ["a", "b", "c", "d"]
>> first
=> "a"
>> rest
=> ["b", "c"]
>> last
=> "d"
>>
But in Ruby, splat operator can also create an array.
>> q=*135
=> [135]
>> q
=> [135]
In the second scenario1, it creates an array. I think this is useful for scenario where the method accepts either a single element or an array.