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 :: devise redirectt after sign up 
Ruby :: rspec add support folder 
Ruby :: PG::DatatypeMismatch: ERROR: column "price" cannot be cast automatically to type numeric HINT: You might need t 
Ruby :: ruby read file 
Ruby :: rails difference in minutes between 2 datetime 
Ruby :: Your Ruby version is 3.0.0, but your Gemfile specified 2.7.4 
Ruby :: Ruby on rails execute query 
Ruby :: require relative ruby 
Ruby :: ruby array to string with commas 
Ruby :: edit file terminal mac24 
Ruby :: rails form_tag 
Ruby :: array string ruby 
Ruby :: how to make a new array ruby 
Ruby :: how to remove nested array brackets ruby 
Ruby :: creating model in ruby on rails 
Ruby :: rails validates_presence_of 
Ruby :: ruby rails where not in 
Ruby :: rails content for head 
Ruby :: change namespace in rails route 
Ruby :: infinite loop ruby 
Ruby :: ruby letters order in string 
Ruby :: ruby if else 
Ruby :: ruby remove nil element in array 
Ruby :: ||= ruby 
Ruby :: my rails server exits automatically and now gives the following error: 
Ruby :: name error on ruby 
Ruby :: rails convert euro to dollar 
Ruby :: how to access active record elements in ruby 
Ruby :: ruby decode base64 
R :: r sort character number 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =