Bird
Raised Fist0
Rubyprogramming~10 mins

Inline if and unless (modifier form) in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print 'Hello' only if the condition is true using inline if.

Ruby
puts 'Hello' [1] condition
Drag options to blanks, or click blank then click option'
Aunless
Bif
Cwhile
Duntil
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'unless' instead of 'if' will run the code when the condition is false.
Using 'while' or 'until' here is incorrect because they are for loops.
2fill in blank
medium

Complete the code to skip printing 'Goodbye' if the condition is true using inline unless.

Ruby
puts 'Goodbye' [1] condition
Drag options to blanks, or click blank then click option'
Awhile
Bif
Cunless
Duntil
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' will run the code when the condition is true, which is the opposite of what is needed.
Using 'while' or 'until' is incorrect here because they are for loops.
3fill in blank
hard

Fix the error in the code to print 'Done' only if the variable 'finished' is true using inline if.

Ruby
puts 'Done' [1] finished == true
Drag options to blanks, or click blank then click option'
Aunless
Bwhile
Cuntil
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'unless' will run the code when 'finished == true' is false, which is incorrect here.
Using 'while' or 'until' causes syntax errors in this context.
4fill in blank
hard

Fill both blanks to print 'Alert!' only if 'warning' is true and skip if 'safe' is true using inline modifiers.

Ruby
puts 'Alert!' [1] warning [2] safe
Drag options to blanks, or click blank then click option'
Aif
Bunless
Cwhile
Duntil
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'unless' with 'warning' will invert the logic incorrectly.
Using 'if' with 'safe' will run code when safe is true, which is opposite of skipping.
5fill in blank
hard

Fill all three blanks to print 'Success' only if 'passed' is true, skip if 'failed' is true, and repeat while 'retry' is true using inline modifiers.

Ruby
puts 'Success' [1] passed [2] failed [3] retry
Drag options to blanks, or click blank then click option'
Aif
Bunless
Cwhile
Duntil
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing 'if' and 'unless' incorrectly changes logic.
Using 'until' instead of 'while' changes loop condition.

Practice

(1/5)
1. What does the following Ruby code do?
puts "Hello" if true
easy
A. Prints "Hello" because the condition is true
B. Prints nothing because the condition is false
C. Raises a syntax error
D. Prints "Hello" only if false

Solution

  1. Step 1: Understand the inline if condition

    The code uses if true, so the condition is always true.
  2. Step 2: Determine the action based on the condition

    Since the condition is true, puts "Hello" runs and prints "Hello".
  3. Final Answer:

    Prints "Hello" because the condition is true -> Option A
  4. Quick Check:

    Inline if with true condition prints output [OK]
Hint: If condition is true, inline if runs the code [OK]
Common Mistakes:
  • Thinking it prints only if condition is false
  • Confusing inline if with inline unless
  • Expecting syntax error for inline if
2. Which of the following is the correct syntax for using inline unless in Ruby?
easy
A. puts "No" unless false
B. puts "No" unless
C. puts "No"unless (false)
D. puts "No" unless: false

Solution

  1. Step 1: Recall inline unless syntax

    Inline unless requires a condition after the keyword, like unless false.
  2. Step 2: Check each option

    puts "No" unless false is valid syntax. puts "No"unless (false) puts "No"unless (false) is invalid due to missing space after the string literal. Options B and D are incomplete or invalid.
  3. Final Answer:

    puts "No" unless false -> Option A
  4. Quick Check:

    Inline unless needs a condition after it [OK]
Hint: Inline unless needs a condition right after it [OK]
Common Mistakes:
  • Omitting the condition after unless
  • Using colon instead of space
  • Adding unnecessary parentheses
3. What is the output of this Ruby code?
count = 0
count += 1 if false
count += 1 unless false
puts count
medium
A. 0
B. 2
C. 1
D. SyntaxError

Solution

  1. Step 1: Evaluate first increment with inline if

    count += 1 if false does not run because condition is false, so count stays 0.
  2. Step 2: Evaluate second increment with inline unless

    count += 1 unless false runs because unless false means if true, so count becomes 1.
  3. Step 3: Print the final count

    puts count prints 1.
  4. Final Answer:

    1 -> Option C
  5. Quick Check:

    Inline if false skips, inline unless false runs [OK]
Hint: Inline unless runs when condition is false [OK]
Common Mistakes:
  • Thinking inline if false runs the code
  • Confusing unless with if
  • Expecting count to be 2
4. Identify the error in this Ruby code:
puts "Done" unless
medium
A. Incorrect use of puts
B. Missing condition after unless
C. Should use if instead of unless
D. No error, code runs fine

Solution

  1. Step 1: Check the inline unless syntax

    Inline unless requires a condition after it to decide when to run the code.
  2. Step 2: Analyze the given code

    The code ends with unless but no condition follows, causing a syntax error.
  3. Final Answer:

    Missing condition after unless -> Option B
  4. Quick Check:

    Inline unless must have a condition [OK]
Hint: Always put a condition after unless [OK]
Common Mistakes:
  • Leaving out the condition after unless
  • Thinking puts usage is wrong
  • Confusing syntax error with runtime error
5. Given the array nums = [0, 1, 2, 3], which code snippet correctly prints only the non-zero numbers using inline unless?
hard
A. nums.each { |n| puts n if n != 0 }
B. nums.each { |n| puts n if n == 0 }
C. nums.each { |n| puts n unless n != 0 }
D. nums.each { |n| puts n unless n == 0 }

Solution

  1. Step 1: Understand the goal

    We want to print numbers except zero, so skip zero and print others.
  2. Step 2: Analyze each option

    nums.each { |n| puts n unless n == 0 } uses puts n unless n == 0, which prints n only if n is not zero. nums.each { |n| puts n if n == 0 } prints only zero, which is wrong. nums.each { |n| puts n unless n != 0 } prints only zero because unless n != 0 means if n == 0. nums.each { |n| puts n if n != 0 } prints non-zero numbers but uses inline if, not unless.
  3. Final Answer:

    nums.each { |n| puts n unless n == 0 } -> Option D
  4. Quick Check:

    Inline unless skips zero, prints others [OK]
Hint: Use unless with condition to skip unwanted values [OK]
Common Mistakes:
  • Confusing unless with if
  • Printing zero instead of skipping
  • Using wrong condition logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard