# Replacing blank character in array with a letter

**URL:** <https://www.ruby-forum.com/t/replacing-blank-character-in-array-with-a-letter/260279>\
**Category:** Uncategorized\
**Created:** [September 20, 2020, 4:34pm UTC](https://www.ruby-forum.com/t/replacing-blank-character-in-array-with-a-letter/260279 "2020-09-20T16:34:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Temceo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@Temceo](https://www.ruby-forum.com/u/Temceo)\
**Post date:** [September 20, 2020, 4:34pm UTC](https://www.ruby-forum.com/t/replacing-blank-character-in-array-with-a-letter/260279/1 "2020-09-20T16:34:44Z")

</div>

Hi, everyone. I am new to Ruby. I am trying to write a hangman programme where a user is presented with is presented with blanks. As they correctly guess a letter, the letter will replace the blank. I have written most of the programme, but cannot work out how to replace the blank with the correctly guessed letter. Can anyone help with this please? Code pasted below, thank you.

# The purpose of the programme is to enable the blanks to be

# replaced by each correctly guessed letter

word = “hello”  
display\_word = “”  
word.size.times do  
display\_word += "\_ "  
end

display\_word = display\_word.split

guess = “h”

display\_word.each\_index do |letter, index|  
if letter == “\_” && word[index] == guess  
display\_word[index] = guess  
end  
end

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [September 20, 2020, 8:24pm UTC](https://www.ruby-forum.com/t/replacing-blank-character-in-array-with-a-letter/260279/2 "2020-09-20T20:24:23Z")

</div>

> but cannot work out how to replace the blank with the correctly guessed letter.

Your line `display_word[index] = guess` is the right idea.  
Your problem is that `display_word` is not what you are expecting.

Try in irb:

```auto
2.7.1 :001 > word = "hello"
2.7.1 :002 > display_word = ""
2.7.1 :003 > word.size.times do 
2.7.1 :004 > display_word += "_"
2.7.1 :005 > end
 => 5 
2.7.1 :006 > display_word = display_word.split
2.7.1 :007 > display_word
 => [" _____"] 
2.7.1 :008 > display_word.size
 => 1 

```

Your issue is with `split`, notice:

```auto
2.7.1 :009 > " ".split
 => [] 
2.7.1 :010 > " ".split //
 => [" ", " ", " "] 

```

Notice the // regexp at the end, to split the spaces. (Not giving an argument to split means it uses the value of $; which is probably not what you want! See [class String - RDoc Documentation](https://docs.ruby-lang.org/en/2.7.0/String.html#method-i-split) )

Incidentally, you could save yourself some trouble by making display\_word an array to start with:

```auto
2.7.1 :012 > display_word = []
2.7.1 :013 > word.size.times do 
2.7.1 :014 > display_word << " "
2.7.1 :015 > end
 => 5 
2.7.1 :016 > display_word
 => [" ", " ", " ", " ", " "]

```

---

<div class="post-metadata">

**Author:** ![Temceo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@Temceo](https://www.ruby-forum.com/u/Temceo)\
**Post date:** [September 21, 2020, 7:08am UTC](https://www.ruby-forum.com/t/replacing-blank-character-in-array-with-a-letter/260279/3 "2020-09-21T07:08:01Z")

</div>

Thank you. This has been really helpful, and I have figured out how to replace a single instance of a letter and I am now working out how to replace multiple occurrences of the same letter, eg “l”.

word = ‘hello’  
display\_word = []  
word.size.times do  
display\_word \<\< "\_ "  
end

guess = ‘h’  
if word.include?(guess)  
correct = word.index(guess)  
display\_word[correct] = guess  
end
