Combining Conditionals with Arrays and Hashes
Combining Conditionals with Arrays and Hashes
Welcome back! Having learned about if-else statements and case expressions, you’re now equipped with essential tools for making decisions in your code. In this lesson, we will take your skills a notch higher by combining conditionals with arrays and hashes. This will allow you to manage more complex scenarios, particularly those involving collections of data.
What You'll Learn
Arrays and hashes are powerful data structures in Ruby, and when combined with conditionals, they open up extensive possibilities. In this lesson, you'll learn how to:
- Access elements within arrays and hashes.
- Use conditional statements to make decisions based on array or hash values.
Consider the following example:
In this example, we access a nested hash to check whether a destination has been visited and respond accordingly. This technique enhances your ability to write dynamic and data-driven programs.
Now, let's look at an example using arrays:
In this example, we check if a specific fruit is within the array of favorite_fruits and print a message based on the result. This demonstrates how you can use arrays combined with conditionals to make decisions based on the presence or absence of elements. Notice that in Ruby, you can use the include? method to check if an array contains a specific element or not. The question mark at the end of the method name is a common convention in Ruby to indicate that the method returns a boolean value.
Notice, that we use the capitalize method to capitalize the first letter of the fruit name. This is a built-in method in Ruby that capitalizes the first letter of a string.
