# RCR: hybrid case expression

**URL:** <https://www.ruby-forum.com/t/rcr-hybrid-case-expression/107996>\
**Category:** Ruby\
**Created:** [June 28, 2007, 7:51am UTC](https://www.ruby-forum.com/t/rcr-hybrid-case-expression/107996 "2007-06-28T07:51:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [June 28, 2007, 7:51am UTC](https://www.ruby-forum.com/t/rcr-hybrid-case-expression/107996/1 "2007-06-28T07:51:33Z")

</div>

I always use a case expression with the target/comparison style, but  
sometimes I find myself wanting to put in a condition. For example:

while( arg=args.shift )  
case arg[0,2]  
when /^-.$/ then short\_opts.push arg[1…-1]  
when (arg == “–foo”) then bar() # cannot do this, except with  
below modifications  
when “–” then long\_opts.push arg[2…-1]  
else other\_args.push arg  
end  
end

It would be great if this was built into the language. If other  
people are interested, perhaps I will write an RCR. Otherwise this  
can actually be implemented now fairly easily now.

module BooleanCaseCompare  
def self.included( into )  
alias\_method : **old\_case\_compare** , :===  
end

def ===( other )  
if other.is\_a? TrueClass or other.is\_a? FalseClass  
**old\_case\_compare** ( other )  
else  
self  
end  
end  
end

class TrueClass; include BooleanCaseCompare end  
class FalseClass; include BooleanCaseCompare end
