arraysrubysplat

How to convert [45,32,56] or "45,32,56" to 42,32,56?


I have a hash from which I want to get specific keys

a={1=>32, 23=>23, 24=>232, 56=>123} 
keys=[23,56]

To get values of this keys from a{}, I'm using function

a.values_at 23,56 # => [23, 232]`

Problem is how do I convert [23,56....] to 23,56


Solution

  • Use splat operator (* before the argument) to convert array to arguments:

    a={1=>32, 23=>23, 24=>232, 56=>123} 
    # => {1=>32, 23=>23, 24=>232, 56=>123}
    keys=[23,56]
    # => [23, 56]
    a.values_at(*keys)
    #=> [23, 123]
    

    See also: