ruby - 如果没有该特定行,我的代码将无法工作。为什么?

标签 ruby chomp

<分区>

puts "Enter the first number"
num1 = Float(gets)

puts "Enter the second number"
num2 = Float(gets)

puts "Enter the operation"
op = gets
op = op.chomp # <--- THIS LINE!

case op 
when "+" then puts num1 + num2
when "-" then puts num1 - num2    
when "*" then puts num1 * num2    
when "/" then puts num1 / num2
end

最佳答案

当输入“+”操作时,您按下了两个键,+return。两者都产生一个字符,结果为 "+\n"。 (\n 是换行符)

chomp 删除换行符。

关于ruby - 如果没有该特定行,我的代码将无法工作。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37165470/

相关文章:

perl - 关于 chomp 的澄清

arrays - 如何使用 chomp

ruby - 如何在 Ruby 中访问 'zip' gem(或 'rubyzip')?

android - 寻找一种将步骤名称和场景写入文件的方法

regex - 从 chomped 行中获取 Perl 分隔符之间的值

Java 如何从字符串中删除回车符 (HEX 0A)?

ruby - 最大程度地使用 pluck

ruby-on-rails - 如何构建我的 RSpec 测试文件夹、文件和数据库?

ruby-on-rails - MalformedCSVError 与 rails CSV (FasterCSV)

ruby - 是否有时会在没有 'gets' 的情况下使用 'chomp' ?