RCR: hybrid case expression

G

Greg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top