E-Learning

Learn Ruby – How to Write a Switch Statement?

 
In programming languages, a “switch statement” is a type of control mechanism used to check if the value of a variable or expression exists and what the program should do from that point. In Ruby, it is implemented using the “case” argument.
 

Case as Switch Statement in Ruby with parameter

The “case” argument always comes with a “when” and “else” associated with it.  It works very similarly to “if-then-else” argument. Bellow, we can see some examples of “case” with a parameter,  and how it behaves according to the type of declaration in each “when” related to it.

case a
When 1
 puts "Single value"
When two, 3
 puts "One of the comma-separated values"
When 4..6
 puts "One of 4, 5, 6"
When 7...9
 puts "One of 7, 8 but not 9"
Else
 puts "Any other thing"
end

 Each “when” declares a situation where the parameter “a” has a possible value that is compared with “a”. These are “cases” which you want to make this assessment. After all the listed cases, it uses the “else” argument to cover all other possibilities.
 

Case as Switch Statement in Ruby without parameter

Case
When b < 3
 puts "Little than 3"
When b == 3
 puts "Equal to 3"
When (1..10) === B
 puts "Something in closed range of [1 ..10]
end

Note that when the parameter is not declared in the “case”, each one of the “when” must have the parameter stated, so that the comparison can be done.

Do you have more to add on this subject? Put your comments in the area below!

If you want to explore other questions, you can check our videos about Ruby. Below are some examples:

 

You can also subscribe to some channels that broadcast videos in Ruby, such as the following:

 johnhamelink

 

 

 rusticflare

 

 

Another cool way to find out interesting things about Ruby is to access our Ruby’s project page on Livecoding.tv!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago