gets.chomp在列表中查找字符串

时间:2015-03-31 18:14:34

标签: ruby list if-statement

是否可以在gets.chompif的{​​{1}}列表中找到字符串?

elsif

1 个答案:

答案 0 :(得分:0)

要查找数组中是否存在元素,可以使用include?。这里:

if mult_of_three.include?(number)

示例:

number = gets.chomp
36
# => "36" 

mult_of_three.include?(number)
# => true 

number = gets.chomp
98
# => "98"

mult_of_three.include?(number)
# => false