Complete the code to print each number in the array.
numbers = [1, 2, 3] numbers.[1] do |num| puts num end
The each method iterates over each element in the array and executes the block for each element.
Complete the code to print each word in uppercase.
words = ['cat', 'dog', 'bird'] words.[1] do |word| puts word.upcase end
The each method is used to perform an action on each element without changing the original array.
Fix the error in the code to correctly iterate over the hash and print keys and values.
person = {name: 'Alice', age: 30}
person.[1] do |key, value|
puts "#{key}: #{value}"
endThe each method correctly iterates over key-value pairs in a hash.
Fill both blanks to create a hash with words as keys and their lengths as values.
words = ['apple', 'banana', 'cherry'] lengths = words.map { |word| [[1], [2]] }.to_h
In Ruby, word.length returns the length of the string word. Use word as key and word.length as value.
Fill all three blanks to create a hash with uppercase words as keys and their lengths as values, only for words longer than 5 letters.
words = ['apple', 'banana', 'cherry', 'date'] lengths = words.select { |word| word.length [3] 5 }.map { |word| [[1], [2]] }.to_h
The key is the uppercase version of the word using word.upcase. The value is the length of the word with word.length. The condition filters words longer than 5 letters using >.