如何在此代码中包含 switch case

How to include switch case in this code

你好我写了这段代码

class Ability
  include CanCan::Ability

  def initialize(employee)
    employee ||= Employee.new
    if employee[:role]== 'SUPER-ADMIN'
      can :manage, :all
    elsif employee[:role]== 'HR'
      can :manage, :Employee
      can :manage, :Interview
    elsif employee[:role]== 'INVENTORY'
      can :manage, :Inventory
    else
      can :read, :all
    end
  end
end 

现在我必须给出 switch case 而不是像这样的 if else 条件

case role
      when "SUPER-ADMIN"
        can :manage, :all
      when "HR"
        can :manage, :Employee
        can :manage, :Interview
      when "INVENTORY"
        can :manage, :Inventory
      when  "Employee"
        can :read, :all
    end

请指导我如何操作。提前致谢

你快到了。您可以对 case 做一些小改动以使其正常工作:

class Ability
  include CanCan::Ability

  def initialize(employee)
    employee ||= Employee.new
    case employee[:role]
    when 'SUPER-ADMIN'
      can :manage, :all
    when 'HR'
      can :manage, :Employee
      can :manage, :Interview
    when 'INVENTORY'
      can :manage, :Inventory
    else
      can :read, :all
    end
  end
end