Forum: Ruby RCR: hybrid case expression

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Greg (Guest)
on 2007-06-28 09:51
(Received via mailing list)
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

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__, :===

  def ===( other )
    if other.is_a? TrueClass or other.is_a? FalseClass
      __old_case_compare__( other )

class TrueClass;  include BooleanCaseCompare end
class FalseClass; include BooleanCaseCompare end
This topic is locked and can not be replied to.