-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex16.rb
executable file
·40 lines (29 loc) · 1.03 KB
/
ex16.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env ruby
# Assign the first argument to variable filename
filename = ARGV.first
# We assign the name of the script, which is stored in $0, to the variable script. I have no idea why so I'm commenting it out.
# script = $0
puts "We're going to erase #{filename}."
puts "If you don't want that, hit CTRL-C (^C)."
puts "If you do want that, gently press RETURN."
print "? "
STDIN.gets
puts "Opening the file..."
# Call the open method on class File with the filename and write options
target = File.open(filename, 'w')
puts "Truncating the file. Goodbye!"
# Truncate the file. Not sure what target.size does
target.truncate(target.size)
puts "Now I'm going to ask for three lines."
print "line 1: "; line1 = STDIN.gets.chomp()
print "line 2: "; line2 = STDIN.gets.chomp()
print "line 3: "; line3 = STDIN.gets.chomp()
puts "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
puts "And finally, we close the file."
target.close()