Complete the code to define a method that returns the square of a number.
def square(num) num [1] num end
The method multiplies the number by itself to get the square.
Complete the code to create an array of squares from 1 to 5 using map.
squares = (1..5).[1] { |n| n * n }
The map method transforms each element by applying the block, returning a new array.
Fix the error in the code to filter even numbers from an array.
evens = [1, 2, 3, 4, 5].[1] { |n| n.even? }
The select method filters elements based on the block's condition.
Fill both blanks to create a hash of word lengths for words longer than 3 characters.
lengths = words.select { |word| word.length > 3 }.map { |word| [ [1], [2] ] }.to_hword.size is valid but not the expected answer here.The hash keys are words, and the values are their lengths using word.length.
Fill all three blanks to create a hash of uppercase words and their lengths for words longer than 4 characters.
result = words.select { |word| word.length [3] 4 }.map { |word| [ [1], [2] ] }.to_hThe keys are uppercase words, values are their lengths, filtered by length greater than 4.