![](/img/trans.png)
[英]`input_data': undefined method `chomp' for nil:NilClass (NoMethodError)
[英]undefined method `chomp' for nil:NilClass (NoMethodError)
我从Zed Shaw的书“困难的学习Ruby”中输入了以下代码
input_file = ARGV.first #this takes the file test.txt
def print_all(f) #reading a line
puts f.read
end
def rewind(f)
f.seek(0)
end
def print_a_line(line_count, f)
current_line
puts "#{line_count}, #{f.gets.chomp}"
end
current_file = open(input_file)
puts "First let's print the whole file:\n"
print_all(current_file)
puts "Now let's rewind, kind of like a tape"
rewind(current_file)
puts "Let's print three line:"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
我得到的错误是'ex20.rb:12:in print_a_line': undefined method chomp' for nil:NilClass (NoMethodError)
ex20.rb:31:in中的'ex20.rb:12:in print_a_line': undefined method chomp' for nil:NilClass (NoMethodError)
任何帮助将不胜感激。 我一个字一个字地跟着他的榜样。
您必须在test.txt
文件中再添加几行(最后,每个方法调用至少要包含三行文本)。
我遇到了同样的问题,因为该课程尚不清楚,但是由于脚本连续打印出三行,因此文件中需要三行文本才能使脚本起作用。
在您的test.txt文件中添加更多行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.