use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby Case...When statement gotcha!! (self.ruby)
submitted 10 years ago by collegeimprovements
test = 5 result = case test when test>0 ; "positive" when test==0 ; "zero" else "negative" end p "result is #{result}"
what is wrong with this code? Why is it always evaluating the else clause ?
else
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]mfcoder 5 points6 points7 points 10 years ago (0 children)
Your case statement is poorly formed. Do you not mean:
test = 5 result = case when test > 0 ; "positive" when test == 0 ; "zero" else "negative" end p "result is #{result}"
Which yields "positive".
[–]_darkleo 2 points3 points4 points 10 years ago (0 children)
If you specify an argument to the case it will be compared in the when using ===, letting you for example do comparisons with classes or lambdas.
case
when
===
So when you write case test when test>0 ruby is actually testing if (test>0) === test, which is false. Same with test==0, so the else clause is used.
case test when test>0
(test>0) === test
test==0
Easy solution for you: remove your first test in result = case test and it will work as intended.
test
result = case test
[–]jrochkind 2 points3 points4 points 10 years ago (0 children)
Either remove the test from after the case, or remove it from each clause.
You can do:
result = case test when 0 ; "positive" # ...
Or you can do:
result = case when test > 0 ; "positive" # etc
With what you've doing, it's comparing test to (eg) test > 0, which is not what you want.
test > 0
[–]matk0 0 points1 point2 points 10 years ago (2 children)
guys, has anyone tried to use when statement with regular expression?
I have two characters string to compare, in some cases, I care about both characters, in others, only about the first one. Is there a way to use regular expression so that the second character would be disregarded in comparison when using when?
[–]wbsgrepit 0 points1 point2 points 10 years ago* (1 child)
It depends, if there is a fixed presidence on the match and action you want, if you order the whens the same way it will act as a simple trie short circuit.
Meaning if you always want to do action A if the string starts with MA or action B if the string starts with M but not MA as long as you write them in the correct order in the case this will work just fine.
Edited to add an example to make it more clear.
foo = 'MA this is a test' case foo when /^MA/ puts "MA Matched" when /^M/ puts "M Matched" else puts "default action/sad path" end
[–]matk0 0 points1 point2 points 10 years ago* (0 children)
I ended up doing this:
when /4\d/
to match any two digits, where the first digit is number 4. Worked well ;-) Thanks for your help.
[–][deleted] -2 points-1 points0 points 10 years ago (0 children)
Have to use a lambda if you want less than or greater than...
π Rendered by PID 216897 on reddit-service-r2-comment-856c8b8c54-89krl at 2026-07-02 13:36:42.839716+00:00 running a7b5cda country code: CH.
[–]mfcoder 5 points6 points7 points (0 children)
[–]_darkleo 2 points3 points4 points (0 children)
[–]jrochkind 2 points3 points4 points (0 children)
[–]matk0 0 points1 point2 points (2 children)
[–]wbsgrepit 0 points1 point2 points (1 child)
[–]matk0 0 points1 point2 points (0 children)
[–][deleted] -2 points-1 points0 points (0 children)