ruby - undefined method `chomp' for nil:NilClass (NoMethodError) -


i have entered following code zed shaw's book on "learning ruby hard way

input_file = argv.first #this takes file test.txt  def print_all(f) #reading 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 whole file:\n"   print_all(current_file)   puts "now let's rewind, kind of tape"  rewind(current_file)  puts "let's print 3 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) 

the error getting 'ex20.rb:12:in print_a_line': undefined method chomp' nil:nilclass (nomethoderror) ex20.rb:31:in `'

any appreciated. have followed example word word.

you have add few more lines test.txt file (at least 3 lines of text each of method calls @ end).

i ran across same issue because lesson isn't clear it, since script prints out 3 lines in row, need 3 lines of text in file script work.


Comments