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

  • Home
  • SEARCH
  • 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 9177387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:18:55+00:00 2026-06-17T17:18:55+00:00

This code does not perform as I expect: case when -> { false }

  • 0

This code does not perform as I expect:

case    
when -> { false } then "why?"        
else "This is what I expect"        
end      
# => "why?"

Neither does this

case
when ->(x) {false} then "why?"  
else "This is what I expect"
end  
# => "why?"

The first then clause is executed in both cases, which must mean that the lambda I supply to the when clause is not being called. I understand that the case equality operator === should be called on whatever the subject of the when clause is. I am wondering what goes on the other side of the === when there is no argument supplied to case. I was thinking it might be nil, but it can’t be:

-> {false} === nil
# => ArgumentError: wrong number of arguments (1 for 0)

->(x) {false} === nil
# => false

This performs as expected and, if it were being executed, would lead to my expected case results or an exception. Can someone explain the results above? It seems that the case equality operator isn’t being used at all, and yet the first when clause is evaluating to true. BTW, I am doing this because the output of a case can be used for variable assignment and it is less wordy then having several elsif clauses. I would like to be able to use arbitrary Procs in a case statement with no argument.

  • 1 1 Answer
  • 0 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-06-17T17:18:56+00:00Added an answer on June 17, 2026 at 5:18 pm
    case    
    when -> { false } then puts "why?"        
    else puts "This is what I expect"        
    end
    
    case    
    when 'cat' then puts "why?"        
    else puts "This is what I expect"        
    end
    
    case    
    when -> { false }.call then puts "why?"        
    else puts "This is what I expect"        
    end
    

    outputs:

    why?
    why?
    This is what I expect
    

    As The Pickaxe ( http://pragprog.com/book/ruby3/programming-ruby-1-9 ) says, there are two forms of case statement.

    The first allows a series of conditions to be evaluated, executing
    code corresponding to the first condition that is true: case when ‹
    boolean-expression ›+ ‹ then › …

    The second form of a case expression takes a target expression
    following the case keyword. case target when ‹ comparison ›+ ‹ then ›
    …

    In your case (case without target) any expression that is not false or nil (such as a Proc or the string ‘cat’) evaluates to true. The Proc is not executed, unless you call it.

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

Sidebar

Related Questions

This code does not throw an error but the query fails, that is, the
This code does not function as expected: // $field contains the name of a
Why this code does not compile (Cygwin)? #include <vector> template <class Ttile> class Tilemap
Could someone tell me why this code does not update the data response? It's
For some reason, this code does not actually draw my bitmap file... or show
I'm newbie in Python. I can't understand why this code does not work: reOptions
foo\r\nbar.replace(/(foo).+/m, bar) Hello. I can not understand why this code does not replace foo
This javascript code does not work in IE8, but works in Firefox and Google
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I'm not really getting how this code does what it does: char shellcode[] =

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.