Run
新建了一个文件temp.dat 存了一个数据,整数 100
PS C:\Users\Administrator\RubyCode> more temp.dat
100
PS C:\Users\Administrator\RubyCode> ruby c2fin2out.rb
Reading Celsius temperature value from data file...
The Celsius is :
100
Saving result to output file 'temp.out'
PS C:\Users\Administrator\RubyCode> more temp.out
212
Code
从文件temp.dat 读取数据,进行温度的计量单位转换,将运算结果输出到文件temp.out
File : c2fin2out.rb
puts "Reading Celsius temperature value from data file..."
num = File.read("temp.dat") # read data from file temp.dat
celsius = num.to_i # convert string to integer number
puts "The Celsius is : ",celsius
fahrenheit = (celsius * 9 / 5) + 32 # computation
puts "Saving result to output file 'temp.out'"
fh = File.new("temp.out","w") # current self is fh
fh.puts fahrenheit # write data into file
fh.close
Another
如果是需要将文件temp.dat的内容直接不修改写到文件temp.out的话,一行代码
File : c2f_another.rb
open('temp.out','w').write(open('temp.dat').read)
Reference
《The Well-Grounded Rubyist, Second Edition》
(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)
1.1.6. Keyboard and file I/O
: ; : ; : ; : ; : ;
: ; : ; : ; : ; : ;
: .v──┸──、.: :
.// / / / /\.:
/ / / / / ゙/ ∧.
⌒⌒⌒⌒┰⌒⌒⌒⌒
:∧ ∧ ┃
.(*゚Д゚)┃
| ⊃¶
し-J
""""""""""""""""""""
http://emoji.vis.ne.jp/kasa.htm