File operations in Ruby

Submitted by Karthikeyan on Oct 20, 2012 - 07:07
File concepts in Ruby This Ruby program opens file in write mode and writes text in it. Then opens the program in read mode and reads the contents. Code:

File.open('test.txt','w') do |f1|
  f1.puts "Welcome to LivetoLearn.in"
  f1.puts "Thank you! Visit Again"
end
File.open('test.txt','r') do |f2|
  while line=f2.gets
    puts line
  end
end
Output:

Welcome to LivetoLearn.in
Thank you! Visit Again