Harry
Harry

Reputation: 1699

Rails set session var from array

I am returning some results from an external source where:

session[:response]['cpm_by_volxs'] == [ 0.0416, 0.0411, ..., 0.0335, 0.0333 ]

How could I then store just the first number 0.0416 as a different session var:

session[:somethingElse] = 0.0416

in a dynamic way (the response will always be different)?

I have tried:

 temp = session[:response]['cpm_by_volxs']
 session[:somethingElse] = temp[temp.index(1)]

UPDATE

Based on Ben Taitelbaum's suggestion:

<%= session[:Response]['cpm_by_volxs'][0] %> == [
<%= session[:Response]['cpm_by_volxs'][1] %> == 0
<%= session[:Response]['cpm_by_volxs'][2] %> == .
<%= session[:Response]['cpm_by_volxs'][3] %> == 0
<%= session[:Response]['cpm_by_volxs'][4] %> == 4
<%= session[:Response]['cpm_by_volxs'][5] %> == 1
<%= session[:Response]['cpm_by_volxs'][6] %> == 6

etc. Any how I can return this in one go? (I am not able to change the response in any way).

Upvotes: 0

Views: 429

Answers (1)

Ben Taitelbaum
Ben Taitelbaum

Reputation: 7403

You can index into an array in ruby just as you would with other languages, so the first number is just session[:response]['cpm_by_volxs'][0], which is the same as temp[0] in your example.

Upvotes: 1

Related Questions