I’m trying to create a two-dimensional hash like the following:
permissions['enrollment'] = ['read' => true, 'create' => true, 'update' => true]
permissions['invoices'] = ['read' => true, 'create' => false, 'update' => false]
This is what I do…
permissions = Hash.new
permissions['enrollment'] = ['read' => true, 'create' => true, 'update' => true]
permissions['invoices'] = ['read' => true, 'create' => false, 'update' => false]
When I “puts” permissions in irb I get this…
{
"enrollment"=>[{"read"=>true, "create"=>false, "edit"=>false}],
"invoices"=>[{"read"=>true, "create"=>false, "update"=>false}]
}
As you can see from the above output, there seems to be a hash inside another hash for permissions[‘enrollment’] [{}]!!!
I want to access the read permissions for enrollment like this: permissions['enrollment']['read'] but according to the puts permission it won’t work and I get this error TypeError: can't convert String into Integer
For me to access the read permissions for enrollment I have to do this: permissions['enrollment'][0]['read'].
How can I make the enrollment read permissions just like this… permissions['enrollment']['read']?
Your syntax is off. Hash is delimited by curly braces. Square brackets are for arrays. You might confuse the two if you’re coming from PHP world. Try this: