[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

RCR: hybrid case expression

Greg

6/28/2007 5:50:00 AM

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