1

我是红宝石的新手。我的过去是在 Java 中的。我正在尝试使用 switch case,在 Ruby 中显然被称为 case 表达式。我想接受用户输入,检查该输入以查看它是否包含某些字符,然后用其他字符替换这些字符。当我尝试运行这个简单的程序时,我得到了很多语法错误,但我不知道为什么。有人可以向我解释一下我是否错误地使用了这个语句,如果我什至可以在这种情况下使用 case 表达式?谢谢你。

    empty_string = true

    while empty_string do
    print "Pleathe enter a thtring: " 
    user_input = gets.chomp
    user_input.downcase!

      case
        when user_input.include? ("s")
            user_input.gsub!(/s/, "th")
        when user_input.include? ("ch")
            user_input.gsub!(/ch/, "th")
        when user_input == ""
            puts "You typed noting! You get nothing sir!"
        when user_input != ""
            empty_string = false
        else
            puts "There are no 's's in your string."
        end

    end

    puts "Zai jian, #{user_input}"

以下是与行和语法错误相关的错误

rb.rb:9 : 语法错误,意外(arg,当 user_input.include 时需要关键字_then 或 ',' 或 ';' 或 '\n'?("s")

rb.rb:11:语法错误,意外的keyword_when,当user_input.include 时期望keyword_end?("ch") ^

rb.rb:13 : 语法错误,意外的keyword_when,当user_input == "" ^ 时期望keyword_end

rb.rb:15:语法错误,意外keyword_when,当user_input != "" ^ rb.rb:17:语法错误,意外keyword_else,期待keyword_end

rb.rb:21:语法错误,意外的keyword_end,期待输入结束

下面是固定代码感谢@Phlip

empty_string = true

while empty_string do
print "Pleathe enter a thtring: " 
user_input = gets.chomp
user_input.downcase!

  case
    when user_input.include?("s")
        user_input.gsub!(/s/, "th")
        empty_string = false
    when user_input.include?("ch")
        user_input.gsub!(/ch/, "th")
        empty_string = false
    when user_input == ""
        puts "You typed noting! You get nothing sir!"
        empty_string = true
    else
        puts "There are no 's's in your string."
    end

end

puts "Zai jian, #{user_input}"

问题是我在 .include? 之后的空间,@Phlip 告诉我 Ruby 对空间敏感。我删除了空白区域并且它起作用了。之后我遇到了布尔值问题并修复了它。它现在按预期工作。

4

1 回答 1

0

我的理解是,您希望向用户询问一个字符串,直到该字符串包含"s"or "ch"。当找到这样的字符串时,您希望在字符串中进行一个或多个替换并打印出包含修改后的字符串的字符串。这是一种类似 Ruby 的方法。

user_input = nil
loop do
  print "Pleathe enter a thtring: " 
  user_input = "cheater" # gets.chomp.downcase

  case user_input
  when /s/
    user_input.gsub!('s','th')
    break
  when /ch/
    user_input.gsub!('ch','th')
    break
  when ""
    puts "You typed noting! You get nothing sir!"
  else
    puts "There are no 's's in your string."
  end
end

puts "Zai jian, #{user_input}"

如果用户输入一个空字符串,"You typed noting! You get nothing sir!"然后"Pleathe enter a thtring: "显示并gets等待另一个条目。

"s"如果用户输入一个不包含's的非空字符串,或者"ch"'s, "Pleathe enter a thtring: "显示并gets等待另一个条目。

显示用户"Chester\n" "Zai jian, chethter"是否输入。如果用户输入"Cheater\N" "Zai jian, theater"则显示。

如果您确实希望替换所有"s"'s 和"ch"'s,请将以下内容替换为前两个when语句。

when /s|ch/
  user_input.gsub!(/s|ch/,'th')
  break

如果完成此操作并"Chester" "thethter"显示用户输入。(该when行可以改为when /s/, /ch/,但我也不喜欢这样,部分原因/s|ch/是仍然需要作为gsub!的第一个参数。)

请注意,case 语句使用Regexp#===方法。因此,我们看到了/s/.===(s) #=> true。Ruby 允许我们编写它/s/ === 'chester'(“语法糖”)。

user_input = <anything>必须在循环之前使其值在循环之后可见。

请参阅内核#loop。对于此方法的其他用途,异常处理在使用枚举器( EnumeratorStopIteration的实例)时非常有用。

===看起来很像==,但它们应该被认为是完全不同的方法。

于 2018-04-19T23:00:52.750 回答