Swacblooms

Making the Moves
Menu
  • Home
  • Motivation
  • Education
  • Programming
  • About
  • Contact
  • Privacy Policy
Home
Programming
Case Statement: Ruby
Programming

Case Statement: Ruby

Sammy February 21, 2021

Hello y’all, in this post I would be writing on how intuitive case statements are in Ruby. Ruby powers some popular websites in the world.
And do you know that one of the languages powering GitHub is Ruby? Ok, let me not sidetrack. Let us get into the use of the case statement in Ruby.

A run-down of what the code above does

  • The gets.chomp method collects input from a user and strips off the new-line at the end of the input.
  • The case statement selects the variable that the when statement uses to compare values.
  • If any of the when statements matches the value of the food variable, it executes the code within it.
  • So let us say you input rice as food, the case statement would print “I ate rice”.
  • A case statement must end with the end keyword.

The case statement above is quite limiting. What happens when a user inputs a value that isn’t handled by the when statement? From the code above nothing would be logged out.

The good thing about the case statement in Ruby is that it supports the use of the else statement.

When the else statement is used within the case statement it handles all the values that aren’t handled by the when statement. The example below explains further:


When a user inputs a value that isn’t handled by the when statement the code within the else statement is executed.

And finally, the when statement allows you to compare a group of values against the case variable.
The code below is an example:


when the when statement is separated with a comma, it checks to see if any of the when values matches the value of the case statement.
So in this case, if the user inputs plantain, “You ate beans or plantain would be printed”

Thanks for reading through. Hope you learnt a little bit about Ruby and its intuitiveness😋

Prev Article
Next Article

Related Articles

reactive variables(apollo client)
Hello guys, in this tutorial, I would be talking about …

Reactive Variables

dataloaders
Hello guys, have you been in a situation that looks …

Speeding up with data loaders

About The Author

Sammy

I am Samson Amaugo. I am a full-stack web developer and I specialize in MERN stack development

Leave a Reply

Cancel reply

Search Site

Recent Posts

  • Easy SSH Via Local DNS
  • Sshing into your computer from anywhere in the world
  • Case Statement: Ruby
  • Voucher Based Ethereum System
  • Making a CRUD API using Azure Functions

Categories

  • EDUCATION
  • Motivation
  • Programming
  • Uncategorized

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

we respect your privacy and take protecting it seriously

RSS Swacblooms

  • Easy SSH Via Local DNS
  • Sshing into your computer from anywhere in the world
  • Case Statement: Ruby
  • Voucher Based Ethereum System
  • Making a CRUD API using Azure Functions
  • Making a Wikipedia chatbot with WIT.AI
  • Making a small Library in Solidity
  • Fauna+Azure Functions
  • MLH Fellowship (Major League Hacking)
  • GraphQL Basic Overview

Swacblooms

Making the Moves
Copyright © 2021 Swacblooms
Swacblooms - Making the Moves