Undefined method 'size' for #<File:text.txt> (NoMethodError)

Posted by user1354456 on Stack Overflow See other posts from Stack Overflow or by user1354456
Published on 2012-12-15T20:48:49Z Indexed on 2012/12/15 23:03 UTC
Read the original article Hit count: 85

Filed under:

This is the code I am running, it does fine until I get to line 15. The command I run is: ruby ex16.rb text.txt. This is a practice sample I'm writing which is meant to be a simple little text editor:

filename = ARGV.first
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, hit RETURN."

print "? "
STDIN.gets

puts "Opening the file..."
target = File.open(filename, 'w')

puts "Truncating the file. Goodbye!"
target.truncate(target.size)

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()undefined method 'size'

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()

© Stack Overflow or respective owner

Related posts about ruby