The Beginners Guide To Ruby If & Else Statements - RubyGuides
The Beginners Guide To Ruby If & Else Statements - RubyGuides
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
1 Learning Ruby
2 Understanding Variables
7 Object-Oriented Programming
Download eBook
1. stock = 10
2.
3. if stock < 1
4. puts "Sorry we are out of stock!"
5. end
This is what needs to be true for the code inside the condition to
work.
“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
== Equals
!= Not equals
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.
But when you want to check for the opposite “not true” (false) there is
two things you can do.
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
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
“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.”
This is saying:
“If the name is equal to ‘David’ and country is equal to ‘UK’ then do
something.”
1. if age == 10 || age == 20
2. end
This means:
1. if age == 10 || 20
2. end
When comparing two strings they must look exactly the same!
1. name = "David"
2. expected_name = "david"
3.
4. if expected_name.downcase == name.downcase
5. puts "Name is correct!"
6. end
By making both strings downcase you can make sure they’ll match if
they have the same content.
For example:
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 ).
1. name = gets.chomp
2. p name
Like this:
1. if 2.even?
2. puts 123
3. end
Is There an Alternative?
If you have an if else expression there is also a shorthand for that.
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.
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.
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