Skip to content
Tags

case statement

July 21, 2012

I always thought that the case statement was used to as a single-pole-multiple-throw switch, or in non electrical engineering terms, and it could be used on the value of only one variable.

I did not know that a case statement could be used to replace complicated if-else logic, say like this:
a = “test”
b = “bar”

p case
when a == “foo”
“got foo”
when b == “bar”
“got bar”
end

=> “bar”

 

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: