Conditional Assignment with ||= in Ruby
📖 Scenario: You are building a simple program to track user preferences for a website. Sometimes users have not set their preferences yet, so you want to assign default values only if the preferences are missing or empty.
🎯 Goal: Learn how to use the conditional assignment operator ||= in Ruby to set default values only when variables are nil or false.
📋 What You'll Learn
Create a variable
theme with the value nilCreate a variable
font_size with the value nilCreate a variable
language with the value "English"Use
||= to assign "Light" to theme only if it is nil or falseUse
||= to assign 12 to font_size only if it is nil or falseUse
||= to assign "English" to language only if it is nil or falsePrint the values of
theme, font_size, and language💡 Why This Matters
🌍 Real World
Websites and apps often need to set default settings for users who haven't chosen their preferences yet.
💼 Career
Understanding conditional assignment helps in writing clean, efficient Ruby code for configuration and data handling.
Progress0 / 4 steps