I want to compare a user input player
with the elements in an array players
:
我想将用户输入播放器与阵列播放器中的元素进行比较:
players = ["KYRIE IRVING", "KEVIN DURANT", "KAWHI LEONARD"]
If player
is in players
disregarding spaces in player
, then I want to move forward; otherwise, I want it to display an error message. For example, if player
is "KYRIE IRVING"
, "Kyrie Irving"
, "kyrie irving"
, or "kyrieirving"
, I want to move forward.
如果玩家在玩家中无视玩家的空间,那么我想继续前进;否则,我希望它显示错误信息。例如,如果玩家是“KYRIE IRVING”,“Kyrie Irving”,“kyrie irving”或“kyrieirving”,我想继续前进。
The following code doesn't work with "kyrieirving"
.
以下代码不适用于“kyrieirving”。
def valid_player(player, fan, players, vote_history)
until players.include?(player) || **players.gsub(/\s+/, "").include?(player)**
puts "\n Oops! That's not a valid player. Please try again."
print "\n Fan #{fan}: "
player = gets.chomp.upcase
end
vote_history << player
end
I could create another array that has all the elements of players without the spaces, then compare with that. I also could create a block in which the gsub
method applies to each element of the array. I'm assuming there's a cleaner solution than either of these two options though.
我可以创建另一个包含没有空格的玩家所有元素的数组,然后与之比较。我还可以创建一个块,其中gsub方法适用于数组的每个元素。我假设有一个比这两个选项中的任何一个更清晰的解决方案。
3 个解决方案
#1
0
A few points
几点
-
puts in ruby includes a newline so you don't need to begin your strings with newline if you call puts.
put in ruby包含换行符,因此如果你调用puts,你不需要用换行符开始你的字符串。
-
I find its easier to store everything in lower case, not upper case. Much easier to read.
我发现它更容易以小写字母存储所有内容,而不是大写字母。更容易阅读。
-
I would use recursion to check for a valid response, not a loop.
我会使用递归来检查有效的响应,而不是循环。
something like this:
像这样的东西:
def valid_player(fan, players, vote_history)
player = gets.strip
player_no_spc = player.gsub(" ","").downcase
players_no_spc = players.map{ |s| s.gsub(" ","")}
if players_no_spc.include?(player_no_spc)
return vote_history << player
else
puts "Oops! That's not a valid player. Please try again."
puts "Fan #{fan}: "
valid_player(fan, players, vote_history)
end
end
#2
0
players = %w|KYRIEIRVING KEVINDURANT KAWHILEONARD|
player_to_check = "Kyrie IRVING"
if p = players.detect? { |p| p == player_to_check.delete(' ').upcase }
puts "Found: #{p}"
else
puts "Oops!"
end
#3
0
The following will match kyrieirving:
以下将匹配kyrieirving:
re = /KYRIE IRVING/mix
The follwoing will match KYRIE IRVING, Kyrie Irving, kyrie irving :
接下来将与KYRIE IRVING,Kyrie Irving,kyrie irving相匹配:
re = /KYRIE IRVING/mi
x stands for ignore whitespace... you may prepare two regex one with x flag and another without and apply both while iterating through your array. Otherwise you will have to apply things (which I think would have been better) that you have mentioned in your post as FORBIDDEN :)
x代表忽略空格...你可以用x标志准备两个正则表达式而另一个没有并在迭代数组时应用两者。否则你将不得不申请你在帖子中提到的FORBIDDEN :)(我认为会更好)
re = /KYRIE IRVING/mi
str = 'KYRIE IRVING, Kyrie Irving, kyrie irving, or kyrieirving,'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
#1
0
A few points
几点
-
puts in ruby includes a newline so you don't need to begin your strings with newline if you call puts.
put in ruby包含换行符,因此如果你调用puts,你不需要用换行符开始你的字符串。
-
I find its easier to store everything in lower case, not upper case. Much easier to read.
我发现它更容易以小写字母存储所有内容,而不是大写字母。更容易阅读。
-
I would use recursion to check for a valid response, not a loop.
我会使用递归来检查有效的响应,而不是循环。
something like this:
像这样的东西:
def valid_player(fan, players, vote_history)
player = gets.strip
player_no_spc = player.gsub(" ","").downcase
players_no_spc = players.map{ |s| s.gsub(" ","")}
if players_no_spc.include?(player_no_spc)
return vote_history << player
else
puts "Oops! That's not a valid player. Please try again."
puts "Fan #{fan}: "
valid_player(fan, players, vote_history)
end
end
#2
0
players = %w|KYRIEIRVING KEVINDURANT KAWHILEONARD|
player_to_check = "Kyrie IRVING"
if p = players.detect? { |p| p == player_to_check.delete(' ').upcase }
puts "Found: #{p}"
else
puts "Oops!"
end
#3
0
The following will match kyrieirving:
以下将匹配kyrieirving:
re = /KYRIE IRVING/mix
The follwoing will match KYRIE IRVING, Kyrie Irving, kyrie irving :
接下来将与KYRIE IRVING,Kyrie Irving,kyrie irving相匹配:
re = /KYRIE IRVING/mi
x stands for ignore whitespace... you may prepare two regex one with x flag and another without and apply both while iterating through your array. Otherwise you will have to apply things (which I think would have been better) that you have mentioned in your post as FORBIDDEN :)
x代表忽略空格...你可以用x标志准备两个正则表达式而另一个没有并在迭代数组时应用两者。否则你将不得不申请你在帖子中提到的FORBIDDEN :)(我认为会更好)
re = /KYRIE IRVING/mi
str = 'KYRIE IRVING, Kyrie Irving, kyrie irving, or kyrieirving,'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end