Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

ruby file write

# Write file
File.open("log.txt", "w") { |f| f.write "#{Time.now} - User logged in
" }
# or
File.write("log.txt", "data...")

# Append file
File.open("foo.txt", "a") { |f| f.write "#{Time.now} - User logged in
" }
# or
File.write("log.txt", "data...", mode: "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 by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #ruby #file #write
ADD COMMENT
Topic
Name
9+3 =