0
0
Rubyprogramming~5 mins

Inline if and unless (modifier form) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the inline if modifier form in Ruby?
It is a way to write an if statement at the end of a line, like: puts "Hello" if condition. The code before if runs only if the condition is true.
Click to reveal answer
beginner
How does the inline unless modifier form work in Ruby?
It runs the code before unless only if the condition is false. For example: puts "No" unless condition prints "No" when the condition is false.
Click to reveal answer
beginner
Rewrite this code using inline if modifier:<br>
if age >= 18
  puts "Adult"
end
puts "Adult" if age >= 18
Click to reveal answer
beginner
What is the difference between inline if and inline unless?
Inline if runs code when the condition is true. Inline unless runs code when the condition is false. They are opposites.
Click to reveal answer
intermediate
Can you use inline if and unless with multiple statements?
No, inline if/unless works best with single statements. For multiple lines, use regular if/unless blocks.
Click to reveal answer
What does this Ruby code do?<br>
puts "Go" if ready
AAlways prints "Go"
BPrints "Go" only if ready is false
CPrints "Go" only if ready is true
DDoes nothing
What will this code print?<br>
puts "Stop" unless moving
if moving is false.
APrints "Stop" only if moving is true
BPrints "Stop"
CPrints nothing
DRaises an error
Which is the correct inline if syntax?
Aputs "Yes" if condition
Bif condition puts "Yes"
Cputs if condition "Yes"
Dputs "Yes" then if condition
Can you write multiple statements after an inline if?
AYes, inside curly braces
BYes, separated by semicolons
CYes, separated by commas
DNo, only one statement is allowed
What does this code do?<br>
puts "Ready" unless not_ready
APrints "Ready" if not_ready is false
BNever prints anything
CAlways prints "Ready"
DPrints "Ready" if not_ready is true
Explain how the inline if modifier works in Ruby and give a simple example.
Think about writing an if statement at the end of a line.
You got /3 concepts.
    Describe the difference between inline if and inline unless modifiers in Ruby.
    Focus on when the code runs depending on the condition.
    You got /3 concepts.