Create single function in Rails that handles multiple function names?

Is it possible to DRY the following code:

def is_user?
  is_role? ROLES[:user]
end
def is_mod?
  is_role? ROLES[:mod]
end
def is_admin?
  is_role? ROLES[:admin]
end

Private
  def is_role?(role)
    self.roles & role == role
  end

Into a single function, yet still have the ability to call the function names as currently (is_user?, is_mod?, etc)


Source: ruby

Leave a Reply