Hunter Maxfield
Hunter Maxfield

Reputation: 109

undefined local variable or method ruby

I recently bought a book that helps a beginner learn ruby. We are building our 1st program and I cannot run the script through terminal without getting an error. All i'm trying to do is open a file.

Here are the details: The script file is simply:

file.open("text.txt").each { |line| puts line }

This script is saved as analyzer.rb and saved in my ruby file called "ruby".

The text.txt file is a plain text file that has the first chapter of oliver twist, nothing special. It is also saved in the ruby file.

Here is what I am typing into terminal to simply run the script:

cd ~/ ruby
ruby analyzer.rb

Here is exactly what I get.

Hunters-MacBook-Pro:ruby huntermaxfield$ ruby analyzer.rb
analyzer.rb:2:in `<main>': undefined local variable or method `“text' for main:Object (NameError)

I have tried to manipulate the code in all sorts of ways, but since I am a beginner it really is just a guess. Any help would be great.

Upvotes: 2

Views: 1876

Answers (2)

Ameliaa
Ameliaa

Reputation: 1

I encountered the same issue on page 84 (Beginning Ruby; From Novice to Professional) of Peter Cooper's book. I definitely recommend using Sublime instead of TextEdit that I initially used. It is all about using REGULAR QUOTES in the code, not smart quotes. Moreover try not to copy paste if you are not sure of your editor!

Upvotes: 0

shivam
shivam

Reputation: 16506

In File 'F' should be in capital. That's the mistake

File.open("text.txt").each { |line| puts line }

Also make sure "text.txt" is in same directory as you ruby script, else provide absolute path like File.open("absolute/path/to/text.txt")

Upvotes: 1

Related Questions