2016-06-14 5 views
0

私の割り当ては、ユーザの入力に"c"または"s"という文字が含まれているかどうかをチェックすることです。私は1人で管理しましたが、私はそれを書く正しい方法を知らないだけです。配列にXまたはYが含まれているかどうかをチェックする方法

私は問題が"s" || "c"であることを知っています。

print 'What can we do for you?' 
user_input = gets.chomp 
user_input.downcase! 

if user_input.empty? 
    puts 'Well you will have to write something...!' 
elsif user_input.include? 's' || 'c' 
    puts "We got ourselves some 's's and some 'c's" 
    user_input.gsub!(/s/, 'th') 
    user_input.tr!('c', 's') 
    puts "The Daffy version, #{user_input}!" 
else 
    print "Nope, no 's' or 'c' found" 
end 

答えて

2

単に

elsif user_input.include?("s") || user_input.include?("c") 

または

%w(s c).any? { |command| user_input.include? command } 
+1

完璧な男:)おかげでたくさん!それはとても簡単ですが、これで私は約1時間スクリーンに接着していました。私は昨日ルビーを始めました。 –

2

のようなもの。これは、正規表現がうまくどこの完璧な例です:

user_input =~ /[sc]/ 
0

あなたは正規表現

を使用することができます
user_input[/s|c/] 
1

か:

(user_input.split('') & %w(s c)).any? 
+2

'user_input'は文字列です。これは' split 'する必要があります: '(user_input.split(' ')&%w(s c))any?'ですが、この方法は効果がありません。 – mudasobwa

+0

@mudasobwaはい、そうです、私の間違い。 –

+0

@CarySwoveland 私はそれを削除したと思った 私に思い出させるためにありがとう! –

0

ありません正規表現:

user_input.count('sc') > 0 
関連する問題