0
0
Rubyprogramming~10 mins

Predicate methods (ending with ?) 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 number is even using a predicate method.

Ruby
number = 4
puts number.[1]
Drag options to blanks, or click blank then click option'
Apositive?
Bodd?
Ceven?
Dzero?
Attempts:
3 left
💡 Hint
Common Mistakes
Using odd? instead of even?
Forgetting the question mark at the end of the method
2fill in blank
medium

Complete the code to check if the string is empty using a predicate method.

Ruby
text = ""
puts text.[1]
Drag options to blanks, or click blank then click option'
Aempty?
Bblank?
Cnil?
Dzero?
Attempts:
3 left
💡 Hint
Common Mistakes
Using nil? which checks for nil, not empty string
Using blank? which is not a standard Ruby method
3fill in blank
hard

Fix the error in the code to check if the array includes the number 5 using a predicate method.

Ruby
numbers = [1, 2, 3, 4, 5]
puts numbers.[1](5)
Drag options to blanks, or click blank then click option'
Ainclude
Bcontains?
Cincludes?
Dinclude?
Attempts:
3 left
💡 Hint
Common Mistakes
Using include without the question mark
Using non-existent methods like includes? or contains?
4fill in blank
hard

Fill both blanks to create a hash with keys as words and values as booleans indicating if the word starts with 'a'.

Ruby
words = ["apple", "banana", "avocado"]
result = words.map { |word| [word, word.[1]('a') && word.[2]('a')] }.to_h
puts result
Drag options to blanks, or click blank then click option'
Astart_with?
Binclude?
Attempts:
3 left
💡 Hint
Common Mistakes
Using include? which checks anywhere in the string, not just the start
Mixing different methods in the two blanks
5fill in blank
hard

Fill the blanks to filter an array of strings to only those that end with 'ing' and check if each is lowercase.

Ruby
words = ["running", "Jumping", "walking", "Talk"]
filtered = words.select { |word| word.[1]('ing') }
result = filtered.map { |word| word.[2] == word }
puts result
Drag options to blanks, or click blank then click option'
Aend_with?
Bdowncase
Cdowncase == word
Ddowncase?
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent method downcase?
Not comparing downcase result to the original word