diff --git a/README.md b/README.md index 06bcd0e..bf48a07 100644 --- a/README.md +++ b/README.md @@ -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 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) - [`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) +- [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) - [Zero Padding](ruby/zero-padding.md) diff --git a/ruby/use-a-case-statement-as-a-cond-statement.md b/ruby/use-a-case-statement-as-a-cond-statement.md new file mode 100644 index 0000000..eecfc61 --- /dev/null +++ b/ruby/use-a-case-statement-as-a-cond-statement.md @@ -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/)