hoow to match a complete word in ruby?

def find_word(text, word)
  regex = /\b#{word}\b/
  matches = text.scan(regex)
  matches.empty? ? "Word '#{word}' not found." : "Word '#{word}' found."
end

text = "Ruby is a programming language. Ruby is fun to learn and use!"
word_to_find = "Ruby"

puts find_word(text, word_to_find)

Explanation:

  1. find_word is a function defined to search for a specific word within a given text.
  2. text variable holds the input text where the word will be searched.
  3. word_to_find variable contains the word you want to find within the text.
  4. \b in the regex variable signifies a word boundary in regular expressions.
  5. #{word} inserts the word to be found within the regular expression pattern.
  6. text.scan(regex) looks for all matches of the given word in the text.
  7. If matches are found, it returns "Word 'Ruby' found."; otherwise, it returns "Word 'Ruby' not found."