I have this block of code, to try and debug what’s coming back to my server:
task = JSON.parse(request.body.read)
puts task.inspect
puts 'description hash: '
puts task[:description]
When this block of code runs, I get:
{"completed" => false, "task_type" => 0, "description"=> "second task"}
description hash:
nil
Is there a different way to access this hash? Because from what I see the inspection shows a description value.
The Problem
In the example you gave, you are defining a hash key of “description” but looking up the :description key instead, which doesn’t exist. The reason is that the first is a String, and the second is a Symbol.
The Solution
Either look up your key with a string, or call #to_s on your subscript if you know your keys are always a String, but your subscript may not be.