0% found this document useful (0 votes)
24 views13 pages

The Beginners Guide To Ruby If & Else Statements - RubyGuides

The document discusses Ruby if/else conditional statements, explaining what they are, the syntax to write them, and how to use comparison operators, multiple conditions, unless statements, else/elsif, and things to watch out for like case-sensitivity when comparing strings.

Uploaded by

saddestmonke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

The Beginners Guide To Ruby If & Else Statements - RubyGuides

The document discusses Ruby if/else conditional statements, explaining what they are, the syntax to write them, and how to use comparison operators, multiple conditions, unless statements, else/elsif, and things to watch out for like case-sensitivity when comparing strings.

Uploaded by

saddestmonke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

The Beginner's Guide


to Ruby If & Else
Statements

https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 1/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 2/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

Today you'll learn exactly what a Ruby if statement is, how to


write a conditional statement in Ruby & why is this so important!

1 Learning Ruby

2 Understanding Variables

3 Working With Collections

4 If / Else Conditional Statements

5 Ruby Loops: Repeating Something


Many Times

6 Thinking Like A Programmer

7 Object-Oriented Programming

 Download eBook

How Do You Make Decisions in


Ruby?
Like these:
https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 3/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

“If the room is too cold turn on the heater”


“If we don’t have enough stock of this product then send an
order to buy more”
“If this customer has been with us for more than 3 years then
send him a thank you gift”

Things like that are what I mean by making decisions.

If something is true (the condition) then you can do something.

In Ruby, you do this using if statements:

1. stock = 10
2.
3. if stock < 1
4. puts "Sorry we are out of stock!"
5. end

Notice the syntax. It’s important to get it right.

The stock < 1 part is what we call a “condition”.

This is what needs to be true for the code inside the condition to
work.

In plain English this is saying:

“If the value of stock is less than 1 then print the ‘out of stock’
message, otherwise do nothing.”

https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 4/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

Types Of Conditions
In the last example I’m using the “less than” symbol < , but there are
other symbols you can use for different meanings.

Here’s a table:

SY M B O L M E A NI NG

< Less than

> Greater than

== Equals

!= Not equals

>= Greater OR equal to

<= Less OR equal to

Notice that we use two equal == symbols to mean equality!

One equals sign = in Ruby means “assignment”, make sure to use ==

when you want to find out if two things are the same.

If you don’t this right you won’t get the expected results.

Ruby Unless Statement


https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 5/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

With an if statement you can check if something is true .

But when you want to check for the opposite “not true” (false) there is
two things you can do.

You can reverse the value with ! .

Code example:

1. if !condition
2. # ...
3. end

Or you can use unless , which is like if , but it checks for “not true”:

1. unless condition
2. # ...
3. end

Remember, using unless in Ruby is just the reverse of using if.

You’re saying “if this is not true, then do this…”.

The If Else Statement


You can also say “if this is NOT true then do this other thing”:

1. if stock < 1
2. puts "Sorry we are out of stock!"
https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 6/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

3. else
4. puts "Thanks for your order!"
5. end

The else part is always optional, but it can help you write more
advanced logic.

You can take this one step further & use an elsif statement:

1. if stock < 1
2. puts "Sorry we are out of stock!"
3. elsif stock == 10
4. puts "You get a special discount!"
5. else
6. puts "Thanks for your order!"
7. end

With elsif you can say:

“If stock is less than 1 print this message, else if stock equals 10 print
this special message, otherwise if none of these are true then print
the thank you message.”

How to Use Multiple


Conditions
If you’d like to write compound conditions, where you are checking if
two things are true at the same time, then this section is for you.

You can do this by using the && (AND) operator:


https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 7/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

1. if name == "David" && country == "UK"


2. # ...
3. end

This is saying:

“If the name is equal to ‘David’ and country is equal to ‘UK’ then do
something.”

You can also use the || (OR) operator:

1. if age == 10 || age == 20
2. end

This means:

“If the age is 10 or 20 then do something.”

Notice how these two operators ( && , || ) allow you to combine


conditions, but they need to be proper conditions.

In other words, you CAN’T do this:

1. if age == 10 || 20
2. end

This is not valid.

You need a full condition on each side ( age == 10 || age == 20 ).


https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 8/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

Things to Watch Out For


Just before we end this lesson I would like to mention a few problems
you may run into & what to do about them.

The first is about comparing strings.

When comparing two strings they must look exactly the same!

Including the “casing”.

This means that “hello” & “Hello” are different words.

You can solve this by making them as equal as possible:

1. name = "David"
2. expected_name = "david"
3.
4. if expected_name.downcase == name.downcase
5. puts "Name is correct!"
6. end

The key here is the downcase method on name .

By making both strings downcase you can make sure they’ll match if
they have the same content.

For example:

“David” becomes “david”, and “david” stays “david”.


https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 9/13
03/04/2024, 12:12
, y
The Beginners Guide to Ruby If & Else Statements - RubyGuides

Now both are “david” so you can compare them.

Special Symbols in Strings


Another problem you may come across related to arrays is “special
symbols”.

These symbols are for things like new lines n , and the tab key t .

The problem is when you try to compare two strings that look the
same, but they have one of these special symbols.

To see these special symbols you will need to use the p method:

1. name = gets
2. p name

Try this code, type something in, and you will notice that name
contains the newline character (which is not normally visible with
puts ).

To remove this character you can use the chomp method.

1. name = gets.chomp
2. p name

Now the newline character ( n ) is gone, which allows you to compare


strings correctly.
https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 10/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

If Construct in One Line


It’s possible to write an if statement using just one line of code.

Like this:

1. puts 123 if 2.even?

Which is the same as:

1. if 2.even?
2. puts 123
3. end

This is a shorthand version which can be useful if you have a simple


condition.

Is There an Alternative?
If you have an if else expression there is also a shorthand for that.

It’s called the ternary operator:

1. 40 > 100 ? "Greater than" : "Less than"

I have another article where you can learn more about how this
works & learn about other useful Ruby operators
https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 11/13
works & learn about otherTheuseful
03/04/2024, 12:12
Ruby operators.
Beginners Guide to Ruby If & Else Statements - RubyGuides

Read that, then practice & review what you learned today.

Take notes. Practice. Repeat.

A very important component of learning is repetition, so do this &


master Ruby if statements to make progress in your journey of
becoming a great Ruby developer.

I know you can do it.

Summary
Conditions allow you to take decisions in your code, this is what
makes your program “think”.

You also learned how to use the if statement & the else statement
to handle different situations where you want to make decisions.

Finally, you learned about a few things to watch out for, like string
“casing” & special symbols.

Now it’s your turn to take action!

 Last Lesson

https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 12/13
03/04/2024, 12:12 The Beginners Guide to Ruby If & Else Statements - RubyGuides

Next Lesson 

Copyright RubyGuides.com

https://www.rubyguides.com/ruby-tutorial/ruby-if-else/ 13/13

You might also like