The conditionals guide
In this section, we are going to talk about conditionals in Ruby. This is a very important component of learning how to code, since conditionals allow our programs to make decisions based on varying data.
Real-world use of conditionals
Imagine that you're building a self-driving car. Your car would have sensors providing information such as GPS coordinates, the location and speed of the vehicles around the car, and the list goes on and on.
All of this data is great; however, how can we use it to make sure our car functions properly? Our program could follow a set of rules: the following are a few basic ones:
- If the speed limit is 40 MPH, ensure to match this speed
- If there is an accident in front of the car, slow down
- If the car has less than 1/4 of fuel then stop at a gas station
Do you notice how each of these requirements evaluates a parameter and changes the behavior of the car based on its value? This is how conditionals work.
At a high-level, conditionals analyze a scenario...