Onl*_*ere 0 ruby string format
puts "Hello! Reading temperature value from data file..."
num = File.read("temp.dat")
puts "The number is #{num}..."
celsius = num.to_i
fahrenheit = (celsius.to_i * 9 / 5) + 32
puts "The Fahrenheit value is: #{ fahrenheit }."
Run Code Online (Sandbox Code Playgroud)
在第三行中,块...之后#{num}以新行打印.我的印象是使用该块将参数传递给显示器,以便更容易格式化.
为什么点到了新的一行?
当您读入文件时,您也会获得换行符.也许你正在寻找的是这个:
celsius = File.readlines("temp.dat").first.to_i
puts "The number is #{celsius}..."
Run Code Online (Sandbox Code Playgroud)
如果你看看你正在阅读的东西,它可能是有道理的:
puts num.inspect
# => "20\n"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
187 次 |
| 最近记录: |