0
0
Rubyprogramming~10 mins

Select/filter for filtering 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 select even numbers from the array.

Ruby
numbers = [1, 2, 3, 4, 5]
even_numbers = numbers.select { |n| n.[1] }
Drag options to blanks, or click blank then click option'
Aeven?
Bodd?
Cnil?
Dzero?
Attempts:
3 left
💡 Hint
Common Mistakes
Using odd? instead of even? selects odd numbers.
Using nil? or zero? causes errors or wrong results.
2fill in blank
medium

Complete the code to filter words longer than 4 characters.

Ruby
words = ['cat', 'elephant', 'dog', 'lion']
long_words = words.select { |word| word.length [1] 4 }
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= selects words shorter or equal to 4 characters.
Using == selects words exactly 4 characters long.
3fill in blank
hard

Fix the error in the code to select numbers divisible by 3.

Ruby
numbers = [3, 5, 9, 10]
divisible_by_three = numbers.select { |num| num [1] 3 == 0 }
Drag options to blanks, or click blank then click option'
Adiv
B%
Cmod
Dremainder
Attempts:
3 left
💡 Hint
Common Mistakes
Using mod or div causes errors because they are not Ruby operators.
Using remainder is a method but requires different syntax.
4fill in blank
hard

Fill both blanks to create a hash of words and their lengths, filtering words shorter than 5 characters.

Ruby
words = ['apple', 'bat', 'car', 'dog']
lengths = words.select { |word| word.length < 5 }.map { |[1]| [[1], [2]] }.to_h
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping keys and values.
Using incorrect variable names.
5fill in blank
hard

Fill all three blanks to select words starting with 'a' and convert them to uppercase.

Ruby
words = ['apple', 'banana', 'avocado', 'cherry']
result = words.select { |[1]| [2].start_with?('[3]') }.map(&:upcase)
Drag options to blanks, or click blank then click option'
Aword
Ca
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong block variable names.
Using wrong starting letter.