Joined June 2012

Erik Straub

Engineer at MadGlory Interactive
MadGlory Interactive
Schenectady, NY

Posted to lambdas in ruby case (switch) over 1 year ago

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}"
75 Karma
370 Total ProTip Views