RCR: hybrid case expression


#1

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