Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 466251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:27:36+00:00 2026-05-12T23:27:36+00:00

For a long time I tried desperately to get this to work, I’ve googled,

  • 0

For a long time I tried desperately to get this to work, I’ve googled, I’ve tinkered, and I’ve asked some local Rubyists (though not pdx.rb, though I really should).

We were trying to do something like this at work:

case user.roles.included? (... magic ...) 
when ['admin', 'editor'] 
  then ...
when ['anonymous'] 
  then ...
end

So you can see how it feels like a case, but because it’s not using === as El noted, it doesn’t work. I know I can use if but it feels like a place where a case should be used.

  • 1 1 Answer
  • 5 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:27:36+00:00Added an answer on May 12, 2026 at 11:27 pm

    when uses the === operator to compare the value given to case against the argument given to when. Also then is unnecessary when appearing on a different line from the when statement The correct code for what you’re trying to do is:

    case x
    when 16
      puts 'hi'
    when Object
      puts 'obj'
    end
    

    As for the new addition to the question:

    case user.roles.included? (... magic ...) 
    when ['admin', 'editor'] 
      ... 
    when ['anonymous'] 
      ... 
    end
    

    This doesn’t work because Array’s === doesn’t map to include. I’m not sure where Array’s === operator comes from or even what it does, but you could override it to provide the functionality you want.

    Judging by the above code you want the case to trigger if one of the users roles matches the array. This will override Array#=== to do just that:

    class Array
      def === other_array
        ! (other_array & self).empty?
      end
    end
    
    case user.roles
    when ['admin', 'editor'] 
      ... 
    when ['anonymous'] 
      ... 
    end
    

    Caveat: Depending on where you override Array#=== this can have unforeseen consequences, as it will change all arrays in that scope. Given that === is being inherited from Object where it is an alias for ==, I’m not expecting it to be a big problem.

    Places where the new === differs from the old ===:

    • New === will return true if either array is a subset or reordering of the other.
    • Old === will only return true if the two arrays are identical (order and contents).

    So far as I know, case/when is the only time === could be implicitly called on an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have search for a long time to get this thing work. What I
I've spent a long time trying different things to get this to work but
I have tried for a very long time now to figure out, why this
I had this problem a long time ago and recently I got some clues
This has had me stuck for a long time and I have tried several
Possible Duplicate: C# Connecting Through Proxy I've tried doing this for a long time
all, I have searched for this problem for long time and tried different methods.
For a long time i have tried to work out the best way to
I have tried a long time to find help regarding a random date in
Long time reader never posted until now. Im having some trouble with Android, im

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.