Ruby’s case statements are pretty cool and more intuitive to the C, C++ counterparts. Each object in the when statement is expected to support the === operator which is invoked with the object given in the case statement. This allows you use Range, Regexp and other objects as long as they support the === operator.
For example all of the following work:
string = "hello"
case string
when /^hello/
puts "matches first when"
when /^world/
puts "matches second when"
end
You can also put multiple match criteria within the same when statement in which case the matches are OR‘d. Well, I want to AND them or ideally just define a Proc that specifies a match. The reason is I have a largish case statement and don’t feel like converting them all into if/elsif/end. I was thinking something like would be pretty handy:
case string
when { |x| x =~ /^hello/ & x.size > 5 }
puts "matches"
end
Well, that doesn’t make the interpreter happy. So here’s another way. First define a matches method that wraps the given block into a Proc and adds the === operator to the eigen class of the object. This just means that are only adding this method to obj and not the Proc class. Remember Ruby classes are open?
def matches &block
obj = Proc.new(&block)
class << obj
def === rhs
self.call(rhs)
end
end
return obj
end
Using this in case statements is now trivial and it reads pretty nicely too:
case string
when matches { |x| x =~ /^hello/ and x.size > 5 }
puts "works!"
end
and voila’.
Update: The point wasn’t about fancy regexes and how I could’ve used a better one, but more about using complex boolean expressions in when statements.

Pingback: salsaritas buffalo ny
Pingback: guaranteed rankings
Pingback: Dallas Advertising
Pingback: Dallas Video Production
Pingback: Dallas Family Portrait Photographer
Pingback: Dallas Wedding Photographers
Pingback: best seo blog
Pingback: cover letters
Pingback: pre workout supplements
Pingback: Ramtha
Pingback: best post workout supplements
Pingback: best bcaa
Pingback: purina coupons
Pingback: LED Lights
Pingback: paranormal activities
Pingback: Porn Discussion
Pingback: read more
Pingback: Free Dell XPS M1530
Pingback: engagement rings uk
Pingback: Coping with Stress
Pingback: Web Hosting Reviews
Pingback: liposuction recovery
Pingback: ingilizce kursu
Pingback: Phil Cannella Complaints
Pingback: link wheels
Pingback: Super Bowl Picks
Pingback: Escort girl Paris
Pingback: Wedding photographers in Miami
Pingback: how to fix eye floaters
Pingback: manchester property investment
Pingback: take fish oil supplements
Pingback: Sri lankan Florist
Pingback: SEO link building
Pingback: best outdoor gps
Pingback: dating advice
Pingback: how to start website
Pingback: how to get lawn care customers
Pingback: parkland florida homes
Pingback: Aleen Bonier
Pingback: get blood out of jeans
Pingback: period while pregnant first month
Pingback: Scott Tucker Payday Loans
Pingback: Scott Tucker Racing
Pingback: Scott Tucker Racing
Pingback: Scott Tucker CBS
Pingback: How To Lose Stomach Fat In Three Steps
Pingback: Bank Owned Homes
Pingback: football training
Pingback: REO Companies
Pingback: brokers price opinion
Pingback: lipo 6 hers reviews
Pingback: electric cigarette
Pingback: steven cymbrowitz
Pingback: best payday loan
Pingback: Orangatang Balut
Pingback: makeup artists melbourne
Pingback: boot camp workout
Pingback: ecommerce seo
Pingback: spinach health benefits
Pingback: motorcycle jackets
Pingback: treating cold sores