Naming a variable dynamically in Ruby

I am trying to perform the following operation.

@i, price_0, price_1, price_2 = 0, 0, 0, 0
   until @i > 2 do
     if trade_history[@i]["type"] == 2
       price_"#{@i}" = (trade_history[@i]["xbt_gbp"]).to_f ##NOT WORKING
     end
     @i += 1;
   end

I cannot find anywhere online where it says that you can dynamically name a variable in Ruby. What I want to be able to do is to extract the prices of the trade_history object whenever they have a type 2. I need to be able to use the prices variables (price_0..2) to make calculations at the end of the loop. Please help! 🙂


Source: json

Leave a Reply