2012-04-28 48 views
0

我试图减少5行到1时上述错误,使用target.write:在'%':阵列不能被强迫Fixnum对象(类型错误)试图减少红宝石的线码

target.write("%s \n %s \n %s \n") % [line1, line2, line3] 
# target.write("\n") 
# target.write(line2) 
# target.write("\n") 
# target.write(line3) 
# target.write("\n") 

的注释部分运行时(一号线为target.write(line1),但这并不

为什么不是第一行一样写注释了半年线这里是整个脚本:

filename = ARGV.first 
script = $0 

puts "We're going to erase #{filename}." 
puts "If you don't want that hit control C" 
puts "If you do want that hit execute or 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 "line1: "; line1 = STDIN.gets.chomp() 
print "line2: "; line2 = STDIN.gets.chomp() 
print "line3: "; line3 = STDIN.gets.chomp() 

puts "I'm going to write these to the file." 

target.write("%s \n %s \n %s \n") % [line1, line2, line3] 
# target.write("\n") 
# target.write(line2) 
# target.write("\n") 
# target.write(line3) 
# target.write("\n") 

puts "And finally we close it" 
target.close() 

回答

0

闭幕在target.write行上的paren位置不正确。尝试

target.write("%s \n %s \n %s \n" % [line1, line2, line3]) 
+0

吉兹,俯瞰/思前想简单的事情。谢谢 – 2012-04-28 22:02:36

0

您的括号是错误的。你有什么:

target.write("%s \n %s \n %s \n") % [line1, line2, line3] 

相当于做:

a = target.write("%s \n %s \n %s \n") 
a % [line1, line2, line3] 

因此,你想要的是:

target.write("%s \n %s \n %s \n" % [line1, line2, line3])