user2517182 user2517182 - 2 months ago 11
Ruby Question

Ruby push a hash into an array with out curly braces

I am trying to create a parameter list for use in a function call.

The function call looks like the following:

render json: @flight, meta: {url: Flight.encrypt(@flight.id.to_s)}


I tried the following:

params = [key1: value1]
params.push(key2: value2)


In hopes of using the splat operator (
*
) to populate the parameter list. However, params looks like the following:

[{key1: value1},{key2: value2}]


Any help would be greatly appreciated.

Edit



I updated the example code to add more context.

The function call looks like the following:

render json: some_json_value, meta: {some_key: some_value}


I tried the following:

params = [json: @some_json_value]
params.push(meta: {some_key: some_value})


In hopes of using the splat operator (
*
) to populate the parameter list. However, params looks like the following:

[{json: @some_json_value},{meta: {some_key: some_value}}]


Any help would be greatly appreciated.

Answer

You method expects an hash (not an array of hashes). Therefore you have to use a hash to build your params:

parameters = { :key1 => value1 }
parameters[:key2] = value2

somefunction parameters