Bird
Raised Fist0
Rubyprogramming~10 mins

Unless for negated conditions 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 a message unless the number is zero.

Ruby
number = 5
puts "Number is not zero" [1] number == 0
Drag options to blanks, or click blank then click option'
Auntil
Bunless
Cwhile
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'unless' will print the message only when number is zero.
Using 'while' or 'until' is for loops, not conditions.
2fill in blank
medium

Complete the code to skip printing when the word is empty using unless.

Ruby
word = "hello"
puts word [1] word.empty?
Drag options to blanks, or click blank then click option'
Aif
Bwhile
Cuntil
Dunless
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' will print only when the word is empty, which is the opposite of what we want.
Using 'while' or 'until' is for loops, not conditions.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword to print unless the number is negative.

Ruby
number = 3
puts "Positive or zero" [1] number < 0
Drag options to blanks, or click blank then click option'
Aunless
Bif
Cwhile
Duntil
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' will print only when the number is negative, which is wrong here.
Using 'while' or 'until' is for loops, not conditions.
4fill in blank
hard

Fill both blanks to create a hash with words as keys and their lengths as values, but only for words longer than 3 letters using unless.

Ruby
words = ["cat", "elephant", "dog", "lion"]
lengths = {}
words.each { |word| lengths[word] = word.length [1] word.length [2] 3 }
Drag options to blanks, or click blank then click option'
Aunless
B>
C<=
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'unless' changes the logic.
Using '>' instead of '<=' in the condition will include wrong words.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase words as keys and their lengths as values, but only for words not empty using unless.

Ruby
words = ["", "apple", "", "banana"]
result = {}
words.each { |word| result[word.[1]] = word.[2] [3] word.empty? }
Drag options to blanks, or click blank then click option'
Aupcase
Blength
Cunless
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'unless' changes the logic.
Forgetting to convert words to uppercase.

Practice

(1/5)
1. What does the Ruby unless keyword do?
easy
A. Runs the code block regardless of the condition
B. Runs the code block only if the condition is true
C. Runs the code block only if the condition is false
D. Runs the code block twice if the condition is false

Solution

  1. Step 1: Understand the purpose of unless

    The unless keyword runs code only when the condition is false, opposite of if.
  2. Step 2: Compare with other options

    Options B and C describe if or unconditional execution, D is incorrect behavior.
  3. Final Answer:

    Runs the code block only if the condition is false -> Option C
  4. Quick Check:

    unless = run if false [OK]
Hint: Think: unless means "if NOT" the condition [OK]
Common Mistakes:
  • Confusing unless with if
  • Thinking unless runs when condition is true
  • Assuming unless runs code twice
2. Which of the following is the correct syntax for using unless in Ruby?
easy
A. unless condition then puts 'Hello' end
B. unless condition : puts 'Hello'
C. unless (condition) { puts 'Hello' }
D. unless condition puts 'Hello' end

Solution

  1. Step 1: Recall Ruby block syntax

    Ruby uses unless condition ... end without parentheses or braces for blocks.
  2. Step 2: Check each option

    unless condition puts 'Hello' end matches correct Ruby syntax. A uses invalid 'then' with unless, B uses colon incorrectly, C uses braces like other languages.
  3. Final Answer:

    unless condition puts 'Hello' end -> Option D
  4. Quick Check:

    Ruby blocks use unless condition ... end [OK]
Hint: Use newline and end, not do or braces with unless [OK]
Common Mistakes:
  • Adding 'do' after unless
  • Using braces {} for unless block
  • Using colon instead of newline and end
3. What is the output of this Ruby code?
count = 3
unless count > 5
  puts 'Count is small'
else
  puts 'Count is large'
end
medium
A. Count is small
B. Count is large
C. No output
D. SyntaxError

Solution

  1. Step 1: Evaluate the condition count > 5

    Given count = 3, the condition 3 > 5 is false.
  2. Step 2: Apply unless logic

    unless runs the first block if condition is false, so it prints 'Count is small'.
  3. Final Answer:

    Count is small -> Option A
  4. Quick Check:

    Condition false -> unless runs first block [OK]
Hint: Remember unless runs when condition is false [OK]
Common Mistakes:
  • Thinking unless runs when condition is true
  • Confusing else block execution
  • Expecting no output
4. Find the error in this Ruby code using unless:
unless x = 10
  puts 'x is not 10'
end
medium
A. unless cannot be used with variables
B. Assignment (=) used instead of comparison (==) in condition
C. Missing end keyword
D. puts statement should be outside unless block

Solution

  1. Step 1: Identify the condition mistake

    The code uses x = 10 which assigns 10 to x, always true, instead of comparing with ==.
  2. Step 2: Understand impact on unless

    Since assignment returns 10 (truthy), unless condition is true, so block won't run. This is likely a bug.
  3. Final Answer:

    Assignment (=) used instead of comparison (==) in condition -> Option B
  4. Quick Check:

    Use == for comparison, not = [OK]
Hint: Use == to compare, = is assignment [OK]
Common Mistakes:
  • Using = instead of == in conditions
  • Thinking unless needs else always
  • Assuming no error with assignment in condition
5. You want to print "Access denied" only when a user is NOT an admin. Which Ruby code using unless is correct?
hard
A. unless user.admin? puts 'Access denied' end
B. unless user.admin? puts 'Access granted' end
C. if !user.admin? puts 'Access denied' end
D. if user.admin? puts 'Access denied' end

Solution

  1. Step 1: Understand the condition needed

    We want to print 'Access denied' only if user is NOT admin.
  2. Step 2: Match condition with unless

    unless user.admin? runs block if user.admin? is false, exactly when user is not admin.
  3. Step 3: Check other options

    unless user.admin? puts 'Access granted' end prints wrong message, C uses if with negation (correct but not using unless), D prints denied when user is admin (wrong).
  4. Final Answer:

    unless user.admin? puts 'Access denied' end -> Option A
  5. Quick Check:

    Use unless for negated condition [OK]
Hint: Use unless for simple negated conditions [OK]
Common Mistakes:
  • Mixing messages with wrong conditions
  • Using if with negation instead of unless
  • Printing denied when user is admin
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