I have a few questions regarding using json file in ruby.
So my JSON file consists of the info below:
{
"2018-12-11": {
"USD": 1.1379,
"JPY": 128.75,
"BGN": 1.9558,
"CZK": 25.845,
"DKK": 7.4641,
"GBP": 0.90228,
"HUF": 323.4,
"PLN": 4.2983,
"RON": 4.6557,
"SEK": 10.297,
"CHF": 1.1248,
"ISK": 140.2,
"NOK": 9.7038,
"HRK": 7.3943,
"RUB": 75.5225,
"TRY": 6.1295,
"AUD": 1.5778,
"BRL": 4.4417,
"CAD": 1.5239,
"CNY": 7.8498,
"HKD": 8.8934,
"IDR": 16625.0,
"ILS": 4.2691,
"INR": 81.8215,
"KRW": 1284.17,
"MXN": 23.0209,
"MYR": 4.7615,
"NZD": 1.6526,
"PHP": 60.009,
"SGD": 1.5617,
"THB": 37.317,
"ZAR": 16.2903
},
"2018-12-10": {
"USD": 1.1425,
"JPY": 128.79,
"BGN": 1.9558,
"CZK": 25.866,
"DKK": 7.4639,
"GBP": 0.90245,
"HUF": 323.15,
"PLN": 4.2921,
"RON": 4.6502,
"SEK": 10.333,
"CHF": 1.1295,
"ISK": 140.0,
"NOK": 9.6885,
"HRK": 7.387,
"RUB": 75.8985,
"TRY": 6.0499,
"AUD": 1.5842,
"BRL": 4.4524,
"CAD": 1.5218,
"CNY": 7.8967,
"HKD": 8.9283,
"IDR": 16671.36,
"ILS": 4.2642,
"INR": 82.7284,
"KRW": 1287.42,
"MXN": 23.132,
"MYR": 4.7671,
"NZD": 1.6581,
"PHP": 60.367,
"SGD": 1.5667,
"THB": 37.525,
"ZAR": 16.3463
}
There are a lot more dates in my json file. I have tried
require 'json'
file = File.read('eurofxref-hist-90d.json')
Parse Data from File.data_hash = JSON.parse(file)
I get an error
rb:7:in <main>': undefined methoddata_hash=' for File:Class (NoMethodError).
I am very new to Ruby and no idea what I am doing. Basically I want to get the information from the json file so I can develop a method that returns the exchange rate between from_currency and to_currency on date as a float.
Parse Data from Filesupposed to be a comment? If so you should put it on its own line and add a#to the start of the line. Otherwise Ruby thinks it's codeParse Data from File.is not intended to be executable code in wherever you got this snippet from. Ruby thinks you are running this:Parse(Data(from(File.data_hash = JSON.parse(file))))which calls a bunch of functions that don't exist.data_hash["2018-12-11"]["USD"]for instance. Read about hashes here: docs.ruby-lang.org/en/2.0.0/Hash.html