This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
We need to figure out which discount we should apply to widget store orders. Is the user ordering 100 widgets or more? 50 to 99? Less than 50? To do this, we're going to need a way to compare numbers.
- We need to check the number of widgets the user is ordering, and then apply an appropriate discount.
- This code will be a little too complicated to embed within a string. So first, let's move the part that calculates a price out to a method.
def ask(question)
print question + " "
gets.chomp # Add ".chomp" here
end
# New method here
def price(quantity)
quantity * 10
end
puts "Welcome to the widget store!"
answer = ask("How many widgets are you ordering?")
number = answer.to_i
total = price(number) # Call new method
puts "For #{number} widgets, your total is: $#{total}"
In order to know which discount to apply, we need to be able to tell if the order quantity is over 100, if it's between 50 and 100, or if it's below 50.
Ruby uses comparison operators to tell whether one value is equal to, greater than, or less than another.
== # equal
!= # not equal
< # less than
> # greater than
<= # less than or equal to
>= # greater than or equal to
Comparison operators return a boolean value. Boolean values are either true or false. They're represented in Ruby code by the words true
and false
. Note that these are not strings, so they're not surrounded by quotes.
true
false
- Comparison operators
$ irb
2.3.0 :001 > quantity = 75
=> 75
2.3.0 :002 > quantity > 50
=> true
2.3.0 :003 > quantity < 50
=> false
2.3.0 :004 > quantity == 75
=> true
- Don't confuse
==
with=
! If you do, you could accidentally change the value a variable holds!
2.3.0 :005 > quantity == 99 # Tests equality.
=> false
2.3.0 :006 > quantity # Value unchanged.
=> 75
2.3.0 :007 > quantity = 99 # Assigns a new value!
=> 99
2.3.0 :008 > quantity # Value changed!
=> 99
-
>=
returns true if the first value is greater than or equal to the second. -
<=
returns true if the first value is less than or equal to the second.
2.3.0 :009 > quantity = 99
=> 99
2.3.0 :010 > quantity >= 50
=> true
2.3.0 :011 > quantity <= 50
=> false
2.3.0 :012 > quantity = 50
=> 50
2.3.0 :013 > quantity >= 50
=> true
2.3.0 :014 > quantity <= 50
=> true
-
!=
- The exclamation point can be read aloud as "not".
- So
!=
can be read aloud as "not equal". - It's the opposite of
==
.
2.3.0 :015 > quantity = 99
=> 99
2.3.0 :016 > quantity != 50
=> true
2.3.0 :017 > quantity == 50
=> false
- All the comparison operators we just showed you work with strings as well.
2.3.0 :018 > string = "zebra"
=> "zebra"
2.3.0 :019 > string < "ant"
=> false
2.3.0 :020 > string > "ant"
=> true
2.3.0 :021 > string == "ant"
=> false
2.3.0 :022 > string == "zebra"
=> true
2.3.0 :023 > string == "ZEBRA"
=> false
2.3.0 :024 > string != "ant"
=> true
- Don't try to compare values of different classes, it won't work.
"3" == 3 # == will always result in false
"4" > 3 # > and < will give an error
- You should only do a comparison if you can convert one class to match the other.
"3".to_i == 3
"4".to_i > 3
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up