ruby on rails - What does file.new("temp.out", "w") line represent? -


i'm learning ruby language , i'm having lot of fun. working on temperature converter file output exercise.

the solution provided below

print "hello. please enter celsius value: " celsius = gets.to_i fahrenheit = (celsius * 9 / 5) + 32 puts "saving result output file 'temp.out'" 

fh = file.new("temp.out", "w")

fh.puts fahrenheit fh.close 

the highlighted part confuses me.
calling file.new create file named "temp.out" , "w" write whatever inputs until fh.close. correct?

thank you!

by default, puts() send output what's called stdout, connected screen. file.new() creates new file assigned variable fh. because created file in write mode, can use fh write stuff file. fh.puts() sends output file assigned variable fh. in other words, bare puts() statement sends output screen, when precede puts() file, output goes file.

you can write statements this:

file.open("temp.out", "w") |f|   f.puts fahrenheit end 

the neat thing writing is: when end statement executes, ruby automatically close file you.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -