Complete the code to get the number of elements in the array.
numbers = [1, 2, 3, 4, 5] count = numbers.[1] puts count
The length method returns the number of elements in an array.
Complete the code to check if the array contains the number 3.
numbers = [1, 2, 3, 4, 5] has_three = numbers.[1](3) puts has_three
The include? method checks if an array has a specific element and returns true or false.
Fix the error in the code to flatten the nested array into a single array.
nested = [[1, 2], [3, 4], [5]] flat = nested.[1] puts flat.inspect
The flatten method returns a new array that is a one-dimensional flattening of the original nested array.
Fill both blanks to create a hash with words as keys and their lengths as values, only for words longer than 3 letters.
words = ["apple", "cat", "banana", "dog"] lengths = words.each_with_object({}) do |word, h| if word.[1] [2] 3 h[word] = word.[1] end end puts lengths
Use length to get the word length and > to check if it is greater than 3.
Fill all three blanks to create a hash with uppercase words as keys and their lengths as values, only for words longer than 3 letters.
words = ["apple", "cat", "banana", "dog"] lengths = words.each_with_object({}) do |word, h| if word.[2] [3] 3 h[word[1]] = word.[2] end end puts lengths
Use .upcase to make keys uppercase, length to get word length, and > to filter words longer than 3 letters.