How do I write a switch statement in Ruby?
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
- gem cleanup shows error: Unable to uninstall bundl
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
- ruby - simplify string multiply concatenation
In Ruby 2.0, you can also use lambdas in
case
statements, as follows:You can also create your own comparators easily using a Struct with a custom
===
(Example taken from "Can procs be used with case statements in Ruby 2.0?".)
Or, with a complete class:
(Example taken from "How A Ruby Case Statement Works And What You Can Do With It".)
If you are eager to know how to use an OR condition in a Ruby switch case:
So, in a
case
statement, a,
is the equivalent of||
in anif
statement.Many other things you can do with a Ruby case statement
You can write case expressions in two different ways in ruby.
1st way
2nd way
case...when
To add more examples to Chuck's answer:
With parameter:
Without parameter:
Please, be aware of the issue that kikito warns.
As stated in many of the above answers, the === operator is used under the hood on case/when statements.
Here is a few extra information about that operator.
Case equality operator: ===
Many of Ruby's built-in classes, such as String, Range, and Regexp, provide their own implementations of the === operator, also known as case-equality, triple equals or threequals. Because it's implemented differently in each class, it will behave differently depending on the type of object it was called on. Generally, it returns true if the object on the right "belongs to" or "is a member of" the object on the left. For instance, it can be used to test if an object is an instance of a class (or one of its subclasses).
The same result can be achieved with other methods which are probably best suited for the job, such as is_a? and instance_of?.
Range Implementation of ===
When the === operator is called on a range object, it returns true if the value on the right falls within the range on the left.
Remember that the === operator invokes the === method of the left-hand object. So (1..4) === 3 is equivalent to (1..4).=== 3. In other words, the class of the left-hand operand will define which implementation of the === method will be called, so the operand positions are not interchangeable.
Regexp Implementation of ===
Returns true if the string on the right matches the regular expression on the left. /zen/ === "practice zazen today" # Output: => true # is similar to "practice zazen today"=~ /zen/
The only relevant difference between the two examples above is that, when there is a match, === returns true and =~ returns an integer, which is a truthy value in Ruby. We will get back to this soon.