Php Switch Statement? Php Switch Case Conditional Statements

Nothing could be simpler và more boring than the case statement. It’s a holdover from C. You use it lớn replace a bunch of ifs. Case closed. Or is it? Actually, case statements in Ruby are a lot richer & more complex than you might imagine. Let’s take a look.

Bạn đang xem: Php switch statement? php switch case conditional statements

Jul 15, 2015

Nothing could be simpler & more boring than the case statement. It's a holdover from C. You use it to replace a bunch of ifs. Case closed. Or is it?

Actually, case statements in Ruby are a lot richer và more complex than you might imagine. Let's take a look at just one example:

case "Hi there"when String puts "case statements match class"end# outputs: "case statements match class"
This example shows that case statements not only match an item's value but also its class. This is possible because under the hood, Ruby uses the === operator, aka. The three equals operator.

A quick tour of the === operator

When you write x === y y in Ruby, you're asking "does y belong in the group represented by x?" This is a very general statement. The specifics vary, depending on the kind of group you're working with.

# Here, the Class.===(item) method is called, which returns true if item is an instance of the class String === "hello" # trueString === 1 # false
Strings, regular expressions và ranges all define their own ===(item) methods, which behave more or less lượt thích you'd expect. You can even địa chỉ cửa hàng a triple equals method lớn your own classes.

Now that we know this, we can vị all sorts of tricks with case.

Matching ranges in case statements

You can use ranges in case statements thanks to the fact that range === n simply returns the value of range.include?(n). How can I be so sure? It's in the docs.

Xem thêm: Bí Quyết Tăng Nhanh Điểm Kiểm Tra Sinh Học 9, Bí Quyết Tăng Nhanh Điểm Kiểm Tra Sinh Học

case 5when (1..10) puts "case statements match inclusion in a range"end# outputs "case statements match inclusion in a range"

Matching regular expressions with case statements

Using regexes in case statements is also possible, because /regexp/ === "string" returns true only if the string matches the regular expression. The docs for Regexp explain this.

case "FOOBAR"when /BAR$/ puts "they can match regular expressions!"end# outputs "they can match regular expressions!"

Matching procs và lambdas

This is kind of a weird one. When you use Proc#===(item), it's the same as doing Proc#call(item). Here are the docs for it. What this means is that you can use lambdas & procs in your case statement as dynamic matchers.

Writing your own matcher classes

As I mentioned above, adding custom case behavior to lớn your classes is as simple as defining your own === method. One use for this might be khổng lồ pull out complex conditional ngắn gọn xúc tích into multiple small classes. I've sketched out how that might work in the example below:

class Success def self.===(item) item.status >= 200 && item.status 300 endendclass Empty def self.===(item) item.response_size == 0 endendcase http_responsewhen Empty puts "response was empty"when Success puts "response was a success"end has your back when it counts.

We"re the only error tracker that combines exception monitoring, uptime monitoring, & cron monitoring into a single, simple to use platform. Our mission: to lớn tame production & make you a better, more productive developer.

Learn more

Starr Horne

Starr Horne is a Rubyist và Chief JavaScripter at When she"s not neck-deep in other people"s bugs, she enjoys making furniture with traditional hand-tools, reading history & brewing beer in her garage in Seattle.

More articles by Starr Horne
“We"ve looked at a lot of error management systems. is head và shoulders above the rest & somehow gets better with every new release.”
Michael Smith

Level up your software development career

Join our community of kick-ass developers as we learn engineering, DevOps, cloud architecture, and bootstrapping remote software companies. Tell me more →

We"re We"ll never send you spam; we will send you cool stuff lượt thích exclusive content, memes, và special swag.