如何使用.include?在 Ruby 中使用散列语句
how to use .include? in Ruby with a hash statement
我如何获得 .include?上班?当用户选择角色时,我希望控制台打印puts ok语句,然后转到if语句。
name = {"1" => "Mario",
"2" => "Luigi",
"3" => "Kirby",
}
puts "Peach's apocalypse, will you survive?"
def character (prompt, options)
puts = "who will you be?"
options = name[1] || name[2] || name[3]
character = gets.chomp.downcase
until character.include? name
end
puts "ok #{name} all three of you run out of peach's castle which has been overrun"
if character = name[1] || name[2] || name[3]
puts ("zombies are in the castle grounds, there are weapons over the bridge")
puts "What do you do, charge through or sneak?"
x = gets.chomp.downcase
if x == "sneak"
puts "oh you died"
if x == "charge through"
puts "the zombies tumbled over the bridge's edge, you made it safe and sound"
else
puts "you did nothing and were eaten alive by Princess Peach"
end
end
end
end
您似乎在对字符串调用 include?
。如果您将其自身的子字符串传递给它,这只会 return 为真。例如:
"Mario".include?("Mar") #=> true
您想对 name
散列中的键数组调用 include?
。你可以这样做:
name.values.include?(character)
或更简洁
name.has_value?(character)
这里有一些关于 the include?
method of the Array class and the include?
method of the string class, as well as the has_value?
method of the Hash class 的文档。
这个程序还有很多需要修改的地方 运行 正如您所期望的那样。这是一个有效的实现:
puts "Peach's apocalypse, will you survive?"
names = {
"1" => "Mario",
"2" => "Luigi",
"3" => "Kirby"
}
def choose_character(character = "", options)
puts = "who will you be?"
options.each do |num, name|
puts "#{num}: #{name}"
end
until options.has_key? character or options.has_value? character
character = gets.chomp.capitalize
end
return options[character] || character
end
name = choose_character(names)
puts "ok #{name} all three of you run out of peach's castle which has been overrun"
puts "zombies are in the castle grounds, there are weapons over the bridge"
puts "What do you do, charge through or sneak?"
case gets.chomp.downcase
when "sneak"
puts "oh you died"
when "charge through"
puts "the zombies tumbled over the bridge's edge, you made it safe and sound"
else
puts "you did nothing and were eaten alive by Princess Peach"
end
上面的答案很好,并且具有很棒的重构功能,但我会使用
character = gets.strip.downcase
相反,因为它也消除了任何潜在的空白。
为了详细说明字符串,'gets' 代表 'get string'(或者至少我被教导如此),所以你通过 'gets' 获得的所有内容都将是一个字符串,直到你进一步转换。考虑一下:
2.2.1 :001 > puts "put in your input"
put in your input
=> nil
2.2.1 :002 > input = gets.strip
5
=> "5"
2.2.1 :003 > input.class
=> String
您必须使用 .to_i 将您的输入转换回整数。
我如何获得 .include?上班?当用户选择角色时,我希望控制台打印puts ok语句,然后转到if语句。
name = {"1" => "Mario",
"2" => "Luigi",
"3" => "Kirby",
}
puts "Peach's apocalypse, will you survive?"
def character (prompt, options)
puts = "who will you be?"
options = name[1] || name[2] || name[3]
character = gets.chomp.downcase
until character.include? name
end
puts "ok #{name} all three of you run out of peach's castle which has been overrun"
if character = name[1] || name[2] || name[3]
puts ("zombies are in the castle grounds, there are weapons over the bridge")
puts "What do you do, charge through or sneak?"
x = gets.chomp.downcase
if x == "sneak"
puts "oh you died"
if x == "charge through"
puts "the zombies tumbled over the bridge's edge, you made it safe and sound"
else
puts "you did nothing and were eaten alive by Princess Peach"
end
end
end
end
您似乎在对字符串调用 include?
。如果您将其自身的子字符串传递给它,这只会 return 为真。例如:
"Mario".include?("Mar") #=> true
您想对 name
散列中的键数组调用 include?
。你可以这样做:
name.values.include?(character)
或更简洁
name.has_value?(character)
这里有一些关于 the include?
method of the Array class and the include?
method of the string class, as well as the has_value?
method of the Hash class 的文档。
这个程序还有很多需要修改的地方 运行 正如您所期望的那样。这是一个有效的实现:
puts "Peach's apocalypse, will you survive?"
names = {
"1" => "Mario",
"2" => "Luigi",
"3" => "Kirby"
}
def choose_character(character = "", options)
puts = "who will you be?"
options.each do |num, name|
puts "#{num}: #{name}"
end
until options.has_key? character or options.has_value? character
character = gets.chomp.capitalize
end
return options[character] || character
end
name = choose_character(names)
puts "ok #{name} all three of you run out of peach's castle which has been overrun"
puts "zombies are in the castle grounds, there are weapons over the bridge"
puts "What do you do, charge through or sneak?"
case gets.chomp.downcase
when "sneak"
puts "oh you died"
when "charge through"
puts "the zombies tumbled over the bridge's edge, you made it safe and sound"
else
puts "you did nothing and were eaten alive by Princess Peach"
end
上面的答案很好,并且具有很棒的重构功能,但我会使用
character = gets.strip.downcase
相反,因为它也消除了任何潜在的空白。
为了详细说明字符串,'gets' 代表 'get string'(或者至少我被教导如此),所以你通过 'gets' 获得的所有内容都将是一个字符串,直到你进一步转换。考虑一下:
2.2.1 :001 > puts "put in your input"
put in your input
=> nil
2.2.1 :002 > input = gets.strip
5
=> "5"
2.2.1 :003 > input.class
=> String
您必须使用 .to_i 将您的输入转换回整数。