Home > database >  How do i fix an implicit conversion for a ruby json code
How do i fix an implicit conversion for a ruby json code

Time:01-04

I'm trying to save a json file with already inputed data with the code snippet below:

def read_rentals
  return [] unless File.exist?('rentals.json')

  rentals_json = JSON.parse(File.read('rentals.json'))
  rentals_json.map do |rental|
    Rental.new(rental['date'], @person[rental['person_index']], @books[rental['book_index']])
  end
end

but I get the error message: block in read_rentals': no implicit conversion from nil to integer (TypeError)

it stopped my script from running. robocop Rental.new line above as the error case.

CodePudding user response:

The JWT.encode method expects a hash of claims. You should simply get the first value of the array and put that in as a parameter. There is no other way around. This is simply the current state of the ruby-jwt implementation.

CodePudding user response:

I found the solution:

in my rentals.json, my person_index was set as null. i changed it to an integer, and the script was working

[
  {
    "date": "04/23",
    "book_index": 0,
    "person_index": 0
  }
]
  • Related