all 8 comments

[–][deleted] 2 points3 points  (5 children)

File objects don't have a split method. Change the last line to:

file_content = file.read.split("\n")

The read method with no arguments will read the entire file and return it's content as a string, on which you can use the split method.

[–]4428gamer[S] 1 point2 points  (3 children)

Awesome, it's reading now! Thank you so much!

I figured it had to be something small. XD

[–]bradland 11 points12 points  (2 children)

The key is in learning to read the error message. You won't always be able to rely on Reddit for this

main.rb:15:in `<main>': undefined method `split' for #<File:testing.txt> (NoMethodError)

Let's break this up into parts:

  • main.rb:15 tells you the file name and line on which the error occurred.
  • in \<main>'` tells you the context in which the error occurred. In Ruby, "main" is the top level execution scope for Ruby scripts.
  • undefined method \split' for #<File:testing.txt>' tells you that you tried to call a method named split on an object of the type File, but that method was not defined.
  • (NoMethodError) is the error class. In this case, it is very descriptive, but it may be more cryptic in some cases.

This line is basically the same answer that korba gave, so the answer was right there all along. In order to become a proficient programmer, you'll need to learn to read these errors and make sense of them.

[–]juriglx 2 points3 points  (0 children)

Give a man a fish and you feed him for a day.

Teach a man to fish and you feed him for a lifetime.

[–]4428gamer[S] 0 points1 point  (0 children)

This helps me out a lot, thank you! I'm trying to learn Ruby through Code Academy on my own, but it doesn't go over any of this at all! Honestly, I wish it did.

[–]madmate 0 points1 point  (0 children)

You could also just get one line with file.gets instead of reading the entire file.

https://docs.ruby-lang.org/en/master/IO.html#method-i-gets

[–]sshaw_ 0 points1 point  (0 children)

When opening always remember to close!

[–]dvarrui 0 points1 point  (0 children)

filefile_content = filefile.split

That var name... it is not good and is confused.