我正在尝试查找数组中的特定字符,但此字符是由用户输入的。如何在Ruby中查找数组中的特定字符
我第一次订购了数组,然后要求用户输入特定的字符,然后我应该看到,如果这个角色在任何该阵列具有
出于某种原因的话存在,如果检查的时候人物的存在,我的“硬编码”的字符,它的工作原理,但如果我尝试去寻找字符,用户已经进入它不工作...
list = [ 'Mom' , 'Dad' , 'Brother' , 'Sister' ]
puts ("Enter the character you would like to find");
character = gets
for i in 0..(list.length - 1)
if (list[i].include?(#{character}))
puts ("Character #{character} found in the word #{list[i]}");
end
非常感谢!
调查使用的['each','select','find'和'任何?'](http://ruby-doc.org/core-1.9.3/Enumerable.html)代替'for'和在数组中使用索引搜索。 'For'不是惯用的Ruby。 –