0
0
Rubyprogramming~10 mins

Compact for removing nil values 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 remove nil values from the array.

Ruby
array = [1, nil, 2, nil, 3]
clean_array = array.[1]
Drag options to blanks, or click blank then click option'
Acompact
Bflatten
Cuniq
Dsort
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'flatten' which changes nested arrays instead of removing nils.
Using 'uniq' which removes duplicates but keeps nils.
Using 'sort' which orders elements but does not remove nils.
2fill in blank
medium

Complete the code to remove nil values from the hash values.

Ruby
hash = {a: 1, b: nil, c: 3, d: nil}
clean_hash = hash.select { |_, v| v [1] }
Drag options to blanks, or click blank then click option'
A>= 0
B== nil
C!= nil
D< 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using '== nil' which keeps only nil values.
Using comparison operators that don't relate to nil.
3fill in blank
hard

Fix the error in the code to remove nil values from the array.

Ruby
numbers = [5, nil, 7, nil, 9]
numbers = numbers.[1]()
Drag options to blanks, or click blank then click option'
Acompact!
Bcompact
Ccompacted
Dremove_nil
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'compact!' without parentheses which can cause syntax errors.
Using non-existent methods like 'compacted' or 'remove_nil'.
4fill in blank
hard

Complete the code to create a hash with word lengths, excluding nil words.

Ruby
words = ["apple", nil, "pear", nil, "plum"]
lengths = {word => 0: word.{BLANK_2}}
Drag options to blanks, or click blank then click option'
A =>
B:
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon ':' instead of '=>', which is invalid in this context.
Using 'length' instead of 'size' (both work but only one is correct here).
5fill in blank
hard

Fill all three blanks to create a hash of uppercase words and their lengths, excluding nils.

Ruby
words = ["dog", nil, "cat", "bird"]
result = { [1]: [2] for word in words if word [3] nil }
Drag options to blanks, or click blank then click option'
Aword.upcase()
Blen(word)
C!=
Dword.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len(word)' which is Python syntax, not Ruby.
Using '==' instead of '!=' to exclude nil values.