mirror of
https://github.com/jbranchaud/til
synced 2026-01-18 22:48:02 +00:00
Add Use A Case Statement As A Cond Statment as a ruby til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_492 TILs and counting..._
|
_493 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -385,6 +385,7 @@ _492 TILs and counting..._
|
|||||||
- [Uncaught Exceptions In Pry](ruby/uncaught-exceptions-in-pry.md)
|
- [Uncaught Exceptions In Pry](ruby/uncaught-exceptions-in-pry.md)
|
||||||
- [`undef_method` And The Inheritance Hierarchy](ruby/undef-method-and-the-inheritance-hierarchy.md)
|
- [`undef_method` And The Inheritance Hierarchy](ruby/undef-method-and-the-inheritance-hierarchy.md)
|
||||||
- [Up And Down With Integers](ruby/up-and-down-with-integers.md)
|
- [Up And Down With Integers](ruby/up-and-down-with-integers.md)
|
||||||
|
- [Use A Case Statement As A Cond Statement](ruby/use-a-case-statement-as-a-cond-statement.md)
|
||||||
- [Who Are My Ancestors?](ruby/who-are-my-ancestors.md)
|
- [Who Are My Ancestors?](ruby/who-are-my-ancestors.md)
|
||||||
- [Zero Padding](ruby/zero-padding.md)
|
- [Zero Padding](ruby/zero-padding.md)
|
||||||
|
|
||||||
|
|||||||
29
ruby/use-a-case-statement-as-a-cond-statement.md
Normal file
29
ruby/use-a-case-statement-as-a-cond-statement.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# Use A Case Statement As A Cond Statement
|
||||||
|
|
||||||
|
Many languages come with a feature that usually takes the name _cond
|
||||||
|
statement_. It is essentially another way of writing an _if-elsif-else_
|
||||||
|
statement. The first conditional in the _cond statement_ to evaluate to try
|
||||||
|
will then have its block evaluated.
|
||||||
|
|
||||||
|
Ruby doesn't have a _cond statement_, but it does have a _case statement_.
|
||||||
|
By using a _case statement_ with no arguments, we get a _cond statement_. If
|
||||||
|
we exclude arguments and then put arbitrary conditional statements after the
|
||||||
|
`when` keywords, we get a construct that acts like a _cond statement_. Check
|
||||||
|
out the following example:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
some_string = "What"
|
||||||
|
|
||||||
|
case
|
||||||
|
when some_string.downcase == some_string
|
||||||
|
puts "The string is all lowercase."
|
||||||
|
when some_string.upcase == some_string
|
||||||
|
puts "The string is all uppercase."
|
||||||
|
else
|
||||||
|
puts "The string is mixed case."
|
||||||
|
end
|
||||||
|
|
||||||
|
#=> The string is mixed case.
|
||||||
|
```
|
||||||
|
|
||||||
|
[source](http://www.skorks.com/2009/08/how-a-ruby-case-statement-works-and-what-you-can-do-with-it/)
|
||||||
Reference in New Issue
Block a user