Complete the code to check if the string contains the word 'Ruby'.
text = "I love programming in Ruby" puts text =~ /[1]/ ? "Found" : "Not found"
The regex /Ruby/ matches the word 'Ruby' in the string, so the output is 'Found'.
Complete the code to find all words starting with 'b' in the string.
text = "bat ball cat box" matches = text.scan(/[1]\w*/) puts matches.join(", ")
The regex /b\w*/ matches words starting with 'b'. The output will be 'bat, ball, box'.
Fix the error in the regex to match an email address.
email = "user@example.com" puts email.match(/\A[\w.+-]+@[1]\.[a-z]{2,}\z/i) ? "Valid" : "Invalid"
The regex part [a-z0-9-]+ correctly matches domain names with letters, digits, and hyphens.
Fill both blanks to create a regex that matches a date in 'YYYY-MM-DD' format.
date = "2024-06-15" pattern = /\A[1]-[2]-\d{2}\z/ puts date.match?(pattern) ? "Date valid" : "Date invalid"
The regex \d{4}-\d{2}-\d{2} matches dates like '2024-06-15' with four digits year and two digits month and day.
Fill all three blanks to create a hash that maps words to their lengths only if length is greater than 3.
words = ["ruby", "is", "fun", "and", "powerful"] lengths = { [1]: [2] for [3] in words if [2] > 3 }
The hash comprehension uses 'word' as key, 'len(word)' as value, iterating over 'word' in words, filtering length > 3.