0
0
Rubyprogramming~10 mins

Truthy and falsy values (only nil and false are falsy) 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 check if the variable is truthy.

Ruby
if [1]
  puts "It's truthy!"
else
  puts "It's falsy!"
end
Drag options to blanks, or click blank then click option'
Atrue
Bnil
Cfalse
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 0 as falsy because in some languages 0 is falsy.
Choosing nil or false as truthy.
2fill in blank
medium

Complete the code to print 'Falsy' only if the value is falsy.

Ruby
value = [1]
if !value
  puts 'Falsy'
else
  puts 'Truthy'
end
Drag options to blanks, or click blank then click option'
Afalse
B42
C"string"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using true or other values that are truthy.
Confusing string or number as falsy.
3fill in blank
hard

Fix the error in the code to correctly check for falsy values.

Ruby
if [1] == false || [1] == nil
  puts 'Falsy value'
else
  puts 'Truthy value'
end
Drag options to blanks, or click blank then click option'
Aval
Bvalue
Cfalse
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Using literal false or nil instead of variable.
4fill in blank
hard

Fill both blanks to create a hash of words and their lengths, but only include words longer than 3 characters.

Ruby
words = ["cat", "house", "dog", "elephant"]
lengths = words.select { |word| [1] [2] 3 }.map { |word| [word, [1]] }.to_h
Drag options to blanks, or click blank then click option'
Aword.length
Bword.size
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causing wrong filtering.
Using word instead of length method.
5fill in blank
hard

Fill all three blanks to create a hash of uppercase words and their lengths, including only words longer than 4 characters.

Ruby
words = ["apple", "bat", "carrot", "dog"]
result = words.select { |w| [2] [3] 4 }.map { |w| [[1], [2]] }.to_h
Drag options to blanks, or click blank then click option'
Aw.upcase
Bw.length
C>
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as key instead of uppercase.
Using less than operator causing wrong filtering.