0
0
Rubyprogramming~15 mins

Truthy and falsy values (only nil and false are falsy) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Truthy and falsy values (only nil and false are falsy)
📖 Scenario: Imagine you are checking a list of items to see which ones are considered true or false in Ruby. In Ruby, only false and nil are treated as false in conditions. Everything else is true.
🎯 Goal: You will create a list of different values, then write code to check which values are truthy or falsy according to Ruby's rules, and finally print the results.
📋 What You'll Learn
Create an array called values with specific elements
Create a variable called falsy_values to hold falsy items
Use a for loop with variable value to check each item
Print the falsy_values array at the end
💡 Why This Matters
🌍 Real World
Understanding truthy and falsy values helps when writing conditions in programs, like deciding if a user input is valid or if a feature should run.
💼 Career
Many programming jobs require knowing how different values behave in conditions to avoid bugs and write clear code.
Progress0 / 4 steps
1
Create the list of values
Create an array called values with these exact elements: false, nil, 0, "" (empty string), [] (empty array), and "hello".
Ruby
Need a hint?

Remember to use square brackets [] to create an array and separate items with commas.

2
Create a variable to hold falsy values
Create an empty array called falsy_values to store values that are falsy.
Ruby
Need a hint?

Use [] to create an empty array.

3
Check each value and collect falsy ones
Use a for loop with variable value to go through each item in values. Inside the loop, use an if statement to check if value is false or nil. If yes, add value to the falsy_values array.
Ruby
Need a hint?

Use for value in values to loop. Use if value == false || value == nil to check falsy values. Use << to add to the array.

4
Print the falsy values
Write a puts statement to print the falsy_values array.
Ruby
Need a hint?

Use puts falsy_values.inspect to print the array in a readable format.