Vladimir Tsukanov
Vladimir Tsukanov

Reputation: 4459

Hash with arrays - get array element

I get hash that contains user role, controller name and list of the controller actions this role can access to.

access = {
    'admin' => [ 'users'    => ['edit','delete'],
                 'messages' => ['show','update']
               ],
     'user' => [ 'index'    => ['index','sign-out'],
                 'messages' => ['show','index']
               ]
}

How can i check what access['admin']['users']['edit'] exists?

Upvotes: 1

Views: 317

Answers (1)

jtbandes
jtbandes

Reputation: 118651

access['admin']['users'].include? 'edit'

However, this may be a problem: you're using ... => ['users'=>['edit','delete'],...] This will create an array with a hash inside. Example:

{'a'=>'b'} #=> {"a"=>"b"}
['a'=>'b'] #=> [{"a"=>"b"}]

So consider using this:

access = {
    'admin' => { 'users'    => ['edit','delete'],
                 'messages' => ['show','update']
               },
     'user' => { 'index'    => ['index','sign-out'],
                 'messages' => ['show','index']
               }
}

Upvotes: 5

Related Questions