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.

 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

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 johnhamelink



rusticflare rusticflare



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

About author

About Author Dr. Michael Jurgen Garbade is the founder of LiveEdu.TV, Kyuda, Education Ecosystem. He is future Venture Capitalist, Future Politician and always on the lookout for the Next Big Challenge. Obtained Masters in business administration and physics, and a Ph.D. in finance with professional work experience in high-paced environments at Fortune 500 companies like Amazon and General Electric. Expertize: Python, PHP, Sencha Touch & C++, SEO, Finance, Strategy & E-commerce. He speaks English and German and has worked in the US, Europe, and Asia. At Education Ecosystem he is the CEO and runs business operations.