Ruby VI | The If, Elsif and the Else 🤷🏽‍♂️

Welcome to the sixth instalment in my Ruby series…If, Elsif & Else!

In programming, you will definitely come across if else statements that fulfil certain conditions and allow our programmes to run certain actions under those conditions.

Today, we will be exploring if else statements in Ruby and in what variations we can implement them in our applications.

Firstly, to help us understand if else statements, let us look at our day to day lives where we naturally use them to determine our next action. Let’s use weather as an example ☁️☂️☀️.

I wake up and see the Sun’s rays glaring through the window and the heat of this celestial body encapsulating the land…It’s SUMMER…HURRAY! 🎉 I get ready for the day and want to bask in the Sun’s heat, so I choose the appropriate garments to wear — t-shirt and shorts of course!

Now if I awoke to the complete opposite atmosphere, where the blistering cold has every living being shivering to the bone and the skies see no spec of sunshine, would I still go outside in a t-shirt and shorts?…I hope you said NO!

So, what am I doing in these conditions and how does it relate to if else?

Let us look it at like this:

If the temperature is above 25 degrees, then wear shorts and a t-shirt — elsif the temperature is between 15 & 25, then wear a hoodie — else wrap up extra warm!

So, how does this look in Ruby code?

temperature = 30if temperature > 25
puts "Wear a t-shirt and shorts!"
elsif temperature.between?(15, 25)
puts "Wear a hoodie."
else
puts "Wrap up warm!"
end
# Wear a t-shirt and shorts!

After the keyword if, we have a condition and ‘if’ the condition is met then we will run the code below it. We use elsif, if we want to consider more than one condition and it too will have a condition after it. We then add else without explicitly stating its condition because it is meant to cover any condition outside of the ones we have stated, and closing off the if else statement with end.

Conditions

Speaking of conditions, here are different types of conditions we can use:

Condition Table | source: Ruby Guides

We can also have multiple conditions for a single if and/or elsif like so:

if temperature > 25 && sky == "blue"
#you decide
end
# Note: we do not always have to have elsif and else in our statements

We have used the && operator to include another condition alongside the first. We could also use the || (or) operator which would allow us to create an action if either condition is met.

Clean Code Please

How can we clean an if statement by putting it on one line? Simple, just put the condition after the action.

puts "It makes sense!" if confusion == false

Here we are saying that if we are not confused (confusion being false) then puts “It makes sense!”. Makes sense? Is your confusion false now? 👀

This only covers a single if statement though, so we would need another option to help us with an if else statement.

Drum rolls please…🥁🥁🥁

Ternary Operator

A ternary operator allows us to write an if else statement on one line without having to explicitly use the if else keywords.

print 123 === 456 ? "YOU KNOW MATHS!" : "You okay there?"# You okay there?

What is happening here? The statement before? is the condition and right after ? is an action that will carry out if the condition is true. If it is false, then the second action after : will carry out. So in other words:

condition ? true : false

Since 123 does not equal to 456, the condition would be false and puts the second statement.

I hope this blog helps you with your if else statements when creating your own applications and finding the best ways to implement different variations of these statements.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Milan Parmar

Milan Parmar

80 Followers

Software Engineer 👨🏽‍💻 | Full Stack Web Development 💻 | Smartphone Tech Enthusiast📱