如何在代码末尾的注释中打印所需的输出I布局?
string = "matthews"
# letters = string.split(//) NOT USING
letter_guess = "t"
if string.include? letter_guess
print "its there"
else
print "its not"
end
# I would then like to ouput this!! --tt----
答案 0 :(得分:2)
如果您只是替换所有不存在的字母,那实际上非常简单:
if (string.include?(letter_guess))
puts "It's there: %s" % string.tr('^' + letter_guess', '-')
else
puts "It's not there."
end
tr
方法非常适合在每个字符的基础上删除或更改内容。在这种情况下,它会替换所有不与(^
)匹配的字母-
。