Commit 18384cf5 by Đường Sỹ Hoàng

Reading&Writng Files

parent 11c21369
Pipeline #111 canceled with stages
in 0 seconds
puts "Hello World!" #
print "Hello World!"
puts "Hello Again" puts "Hello Again"
puts "I like typing this." puts "I like typing this."
puts "This is fun" puts "This is fun"
puts "Yay! Printing." puts "Yay! Printing."
puts "I'd much rather you 'not'." puts "I'd much rather you 'not'."
puts 'I "said" do not touch this.' puts 'I "said" do not touch this.'
\ No newline at end of file
filename = ARGV.first
puts "We're going to erase #{filename}"
puts "If you don't want that, hit CTRL-C (^C)."
puts "If you do want that, hit RETURN."
$stdin.gets
puts "Opening the file..."
target = open(filename, 'w')
puts "Truncating the file. Goodbye!"
target.truncate(0)
puts "Now I'm going to ask you 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 it."
target.close
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment