Question - How to open a file in Ruby?
Answer -
A Ruby file can be created using different methods for reading, writing or both.
There are two methods to open a file in Ruby.
File.new method : Using this method a new file can be created for reading, writing or both.
File.open method : Using this method a new file object is created. That file object is assigned to a file.
Difference between both the methods is that File.open method can be associated with a block while File.new method can't.
Syntax:
f = File.new("fileName.rb")
Or,
File.open("fileName.rb", "mode") do |f|