Complete the code to check if the string contains only digits.
if str =~ /\A[1]+\z/ puts "Only digits" else puts "Contains other characters" end
The pattern \d matches any digit character. Using \A\d+\z ensures the entire string is digits only.
Complete the code to find all whitespace characters in the string.
matches = str.scan(/[1]/)
puts matchesThe pattern \s matches any whitespace character like spaces, tabs, or newlines.
Fix the error in the regex to match a string that starts with a letter.
if str =~ /^[1]/i puts "Starts with a letter" else puts "Does not start with a letter" end
The pattern [a-z] matches any letter from a to z. The i flag makes it case-insensitive.
Fill both blanks to create a hash with words as keys and their lengths as values, only for words longer than 3 characters.
lengths = words.each_with_object({}) { |word, h| h[word] = word.[1] if word.[2] 3 }In Ruby, word.size returns the length of the word. The condition word.size > 3 filters words longer than 3 characters.
Fill all three blanks to create a hash with uppercase words as keys and their lengths as values, only for words containing digits.
result = words.each_with_object({}) { |word, h| h[word.[1]] = word.[2] if word =~ /[3]/ }word.upcase converts the word to uppercase for the key. word.size gives the length for the value. The regex /\d/ checks if the word contains any digit.