0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby学習#27 Writing Files

Posted at

Jim, Sales
Andy, Sales
Kelly, Customer Service
Creed, Quality Assurance
Michaes, Manager

stack overflow Questions Ruby IO

mode meaning

r read-only
w write-only, truncates existing file to zero length or creates a new file for writing
a write-only, starts at end of file if file exists, otherwise creates a new file for reading and writing

File.open("employees.txt", "a") do |file|
file.write("\nOscar, Accounting")
end

Jim, Sales
Andy, Sales
Kelly, Customer Service
Creed, Quality Assurance
Michaes, Manager
Oscar, Accounting

File.open("employees.txt", "w") do |file|
file.write("\nOscar, Accounting")
end

Oscar, Accounting

File.open("index.html", "w") do |file|
file.write("

Hello

")
end

index.html にHello Worldできてる。

from stackoverflow
r+ Read-write, starts at beginning of file.

File.open("index.html", "r+") do |file|
file.readline()
file.write("Hi")
end

Jim, Sales
Hidy, Sales
Kelly, Customer Service
Creed, Quality Assurance
Michaes, Manager
Oscar, Accounting

File.open("index.html", "r+") do |file|
file.readchar()
file.write("Hi")
end

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?