Last Updated: February 25, 2016
· alxndr

lambdas in ruby case (switch)

A little class that has code and message attributes (ignore the bizarre HTML link, I can't prevent Markdown from putting it in?):

class Response
  attr :code, :message
  def initialize(c, m)
    @code, @message = c, m # the <a> should not be here...

...and a method that analyzes those attributes. The interesting part is in a case, where two of the whens use some local methods that return lambdas:

def analyze(resp)
  def code_is(c)
    lambda { |r| r.code == c }

  def message_is(m)
    lambda { |r| r.message == m }

  case resp
  when code_is(200)
    "we are good to go"
  when message_is("Not Modified")
    "not a 200, but it's okay anyway"
    "uh oh, we got a problem... #{resp.inspect}"

Now let's see it in action:

response =, "whatever")
puts "first up", analyze(response), ""

response =, "Not Modified")
puts "next", analyze(response), ""

response =, "No Way")
puts "and finally", analyze(response)

This works because the whens do a ===, which lambdas/Procs use to run themselves.


3 Responses
Add your response

Another way of doing this would be to set constants to lambdas and use them as matchers, such as this:

SUCCESS = ->(response) { response.code == 200 }
NOT_MODIFIED =->(response) { response.message == 'Not Modified' }

case response
when SUCCESS then "we are good to go"
when NOT_MODIFIED then "not a 200, but it's okay anyway"
  "uh oh, we got a problem... #{response.inspect}"
over 1 year ago ·

@brickattack Yeah, very nice!

over 1 year ago ·

So the point is you don't need because the === operator (which is what gets called by the case statement under the hood) invokes that on the lambda by default?

over 1 year ago ·