Question : A Ruby write to file example
Answered by : tsboh
# open and write to a file with ruby
open('myfile.out', 'w') do |f| f.puts "Hello, world."
end
# an alternative approach:
open('myfile.out', 'w') do |f| f << "Hello, world.\n"
end
Source : | Last Update : Thu, 25 Jun 20
Question : ruby file write
Answered by : mysterious-mantis-2t9yeiqzzhwz
# Write file
File.open("log.txt", "w") { |f| f.write "#{Time.now} - User logged in\n" }
# or
File.write("log.txt", "data...")
# Append file
File.open("foo.txt", "a") { |f| f.write "#{Time.now} - User logged in\n" }
# or
File.write("log.txt", "data...", model: "a")
# Other Variants
File.open("out.txt", [your-option-string]) {|f| f.write("write your stuff here") }
# where your options are:
# r - Read only. The file must exist.
# w - Create an empty file for writing.
# a - Append to a file.The file is created if it does not exist.
# r+ - Open a file for update both reading and writing. The file must exist.
# w+ - Create an empty file for both reading and writing.
# a+ - Open a file for reading and appending. The file is created if it does not exist.
Source : https://stackoverflow.com/a/7915881/473923 | Last Update : Fri, 09 Sep 22
Question : ruby read file
Answered by : christopher-olson
file = File.open("yourPath.txt")
results = file.read
Source : | Last Update : Wed, 24 Jun 20