【发布时间】:2017-02-02 22:50:08
【问题描述】:
我对 ruby 很陌生,所以请耐心等待...
str = gets
exit if str.nil? || str.empty?
str.chomp!
temp, scale = str.split(" ")
我的查询如下:
鉴于gets 只会返回并包括cr,为什么要测试空字符串?
如果您测试以下内容:
puts nil.to_s.empty?
puts "".to_s.empty?
puts "".length #the empty string : equates to 0
puts nil.to_s.length #the 'to string' method: equates to 0
两者都将评估为真并且长度为零。但是,如果只有 cr,流中唯一的就是 回车 本身。 str 在下面的长度如果你只是按回车键就会有 1。
print "enter a string or hit the enter key : "
str = gets
puts str.length
puts str
此外,ruby 中的nil 是一个对象。我到底是如何从stdin 捕获那个的?
我现在看到chomp! 在文本中放错了位置,作者的错误不是我的:
str = gets
str.chomp!
exit if str.nil? || str.empty? #test for zero length string will now work
temp, scale = str.split(" ")
当然,我来自 java 和 一些 common lisp,因此可能过于陈旧而无法理解这一点,但我仍然不明白nil 的测试在这种情况下是否合适。也许流中存在一些概念上的差异,这些差异比我的阅读要远。提前致谢。
编辑:
为了消除一些混乱,这里重新编写了代码,仅更改了 chomp! 语句的位置:
#temperature-converter.rb
#code changed by poster, otherwise as written by author
print "Please enter a temperature and scale (C or F) : "
STDOUT.flush #self explanatory....
str = gets
str.chomp! #new placement of method call -- edit by poster
exit if str.nil? || str.empty?
#chomp! original position -- by author
temp, scale = str.split(" ")
abort "#{temp} is not a valid number." if temp !~ /-?\d+/
temp = temp.to_f
case scale
when "C", "c"
f = 1.8 * temp + 32
when "F", "f"
c = (5.0/9.0) * (temp - 32)
else
abort "Must specify C or F."
end
if f.nil?
puts "#{c} degrees C"
else
puts "#{f} degrees F"
end
还有输出:
=>ruby temperature-converter.rb
Please enter a temperature and scale (C or F) : 30 c
86.0 degrees F
=>ruby temperature-converter.rb
Please enter a temperature and scale (C or F) : j c
j is not a valid number.
=>ruby temperature-converter.rb
Please enter a temperature and scale (C or F) : 30 p
Must specify C or F.
=>ruby temperature-converter.rb
Please enter a temperature and scale (C or F) : #just the enter key
=> #did this just exit?!
但是我选择的答案是正确的,如果你使用 Ctl+D(U) 或 Ctl+Z(W)
可以模拟 eof 并抛出 in '<main>': undefined method 'chomp!' for nil:NilClass (NoMethodError) is 错误。尽管如此,字符串永远不会为空,所以检查条件对我来说没有意义。
【问题讨论】:
标签: ruby oop conceptual