Search⌘ K
AI Features

Support the Repeated Letters

Explore techniques for handling repeated letters in Ruby while building a hangman game. Understand how to identify and replace multiple occurrences of letters within a word to enhance game functionality and user experience.

The issue of repeated letters

So far, we’ve left aside the issue of repeated letters. It’s now time to address this problem.

Once again, we'll use a test file (test_word_info.rb). The contents of the file are shown below:

Ruby
#test_word_info.rb
require_relative 'word_info'
puts word_info('palladium', ['p'])
puts word_info('palladium', ['p', 'l'])
puts word_info('palladium', ['p', 'l', 'a'])

We used the word “palladium” because it contains two interesting cases:

  • Two identical letters following each other.

  • Two identical letters not following each other.

We also see on lines 3–5 how we can send a collection (marked by the opening and closing square brackets). Here, the collection that we have contains the letters P , L and A as its elements. We can see this in line 5. ...