Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

A Ruby write to file example

# 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.
"
end
Comment

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.
Comment

PREVIOUS NEXT
Code Example
Ruby :: rails api only with postgress and rspec 
Ruby :: ruby lowercase 
Ruby :: hashwithindifferentaccess ruby 
Ruby :: how drop model rails 
Ruby :: ruby remove unsafe file characters 
Ruby :: rails prepare testing db 
Ruby :: button in rails 
Ruby :: ruby memory location 
Ruby :: in query postgres jsonb rails 
Ruby :: ruby min 2 numbers 
Ruby :: ruby on rails rollback migration 
Ruby :: ruby regexp match all 
Ruby :: how to force exit server in rails 
Ruby :: rails remove column 
Ruby :: rails date format dd/mm/yyyy 
Ruby :: ruby get the number of same element in array 
Ruby :: default value rails migration 
Ruby :: form feild rails helper 
Ruby :: rails clear log files 
Ruby :: rails find_by 
Ruby :: rails params require check exists 
Ruby :: ruby array of symbols shorthand 
Ruby :: While executing gem 
Ruby :: ruby check if path is a directory 
Ruby :: resources rails 
Ruby :: rails check if object is new 
Ruby :: Rails checkbox checked/unchecked values 
Ruby :: include module in rails different folder in rails 
Ruby :: ruby puts object 
Ruby :: singning in using username rails 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =