best way to filter two strings into 1 in ruby

arrays, filter, hidden-field, loops, ruby

Solution

All of the solutions so far revolve around arrays, but don't forget a string is basically a character array anyway. Just use strings:

word = 'busboi'
guesses = 'bs'

word.tr('^'+guesses, '-')
# => "b-sb--"

The `String#tr` method converts all letters in the first argument to the mapping in the second argument, so you can do things like ROT13, simple cyphers and such, or in this case use the negation feature `^` to invert the first set and replace all non-matching characters.

Problem

I am trying to figure out a way to filter two arrays into one based on guessing the letters within one o them.. so basically hangman. But if I had ``` word_array = ["b", "u", "s", "b", "o", "i"] hidden_array = Array.new(word_array.length, "-") p hidden_array ``` I would want to then print to the console ["b", "-", "-", "b", "-", "-"] if "b" were guessed. What would be a good beginner way to create this array that will change over time? Should it maybe be a hash? Thanks!

Original source