0
0
Rubyprogramming~10 mins

Nil as the absence of value in Ruby - Interactive Code Practice

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

Complete the code to assign nil to the variable.

Ruby
value = [1]
Drag options to blanks, or click blank then click option'
ANone
Bnil
Cundefined
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'null' or 'None' which are from other languages.
Using 'undefined' which is not a Ruby keyword.
2fill in blank
medium

Complete the code to check if the variable is nil.

Ruby
if value.[1]?
  puts "Value is nil"
end
Drag options to blanks, or click blank then click option'
Anil
Bempty
Cnull
Dnone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'empty?' which checks if a collection has no elements.
Using 'null?' or 'none?' which do not exist in Ruby.
3fill in blank
hard

Fix the error in the code to assign nil correctly.

Ruby
result = [1]
Drag options to blanks, or click blank then click option'
ANull
BNil
Cnil
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using capitalized versions like 'Nil' or 'Null'.
Using uppercase 'NULL' which is not Ruby syntax.
4fill in blank
hard

Fill both blanks to create a hash with keys and nil values for missing data.

Ruby
person = { name: "Alice", age: [1], city: [2] }
Drag options to blanks, or click blank then click option'
Anil
B"unknown"
D"none"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings like 'unknown' or 'none' instead of nil.
Mixing string and nil values inconsistently.
5fill in blank
hard

Fill all three blanks to return a default value if the variable is nil.

Ruby
default = "Guest"
name = [1] || [2] if [3].nil?
Drag options to blanks, or click blank then click option'
Ainput_name
Bdefault
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not checking for nil properly.