Reputation: 45943
make_api_call
is an expensive resource.
response = make_api_call
case response
when Net::HTTPSuccess
response.body
else
'not found'
end
Is there a way to access the "case object" so that:
case make_api_call
when Net::HTTPSuccess
???.body
Upvotes: 2
Views: 126
Reputation: 37617
I don't think there's a way to get to the argument of the case
. I didn't see anything in Binding
. But you can assign to a local and pass it to the case
all in one go like this:
case response = make_api_call
when Net::HTTPSuccess
response.body
else
'not found'
end
I don't actually recommend this. (I'd just write it as in the question.) It doesn't scope response
to the case
and it causes a Ruby warning (although so do a lot of not genuinely incorrect things). But people whose code standards allow or encourage assignments in conditionals might prefer it to separate assignment.
Upvotes: 1