-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.rb
More file actions
21 lines (17 loc) · 883 Bytes
/
files.rb
File metadata and controls
21 lines (17 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#Reading files
# File.open("myfile.txt", "r") do |txtfile|
#puts txtfile.read # read the whole file as string
#puts txtfile.readline # read the line from the file and move the carrige to the next line
#puts txtfile.readchar # read a character from file
# puts txtfile.readlines[1..2] # read all file line by line in array
# end
#Writing files
# File.open("myfile.txt", "a") do |txtfile| #a - adding information to the file nondestructively or create a new file
# txtfile.write("\nExtra line")
# end
#File.open("index.txt", "w") do |file| #w - writing information to the file destructively or create a new file
# file.write("\nBigger Extra line 2")
#end
File.open("myfile.txt", "r+") do |txtfile| #r+ - reading and writinging information from/to the file destructively or create a new file
txtfile.write("\nExtra line")
end