I have a hash of strings
navigable_objects = { 'Dashboard' => root_path,
'Timesheets' => timesheets_path,
'Clients' => clients_path,
'Projects' => projects_path,
}
I want to convert them to another hash, where the key is again the key, but the value is either the "active" string or the empty string, depending on whether the current controller name contains the key.
For example, let's say that the current controller name is "ClientsController". I should get the result:
{ 'Dashboard' => '',
'Timesheets' => '',
'Clients' => 'active',
'Projects' => ''
}
Here's how I do it now:
active = {}
navigable_objects.each do |name, path|
active[name] = (controller.controller_name.include?(name)) ? 'active' : '')
end
I feel that while this works, is there a better way to do this in Ruby, perhaps using injector each_with_objects?
source
share