# Keep getting main:Object (NameError)

**URL:** <https://www.ruby-forum.com/t/keep-getting-main-object-nameerror/259515>\
**Category:** Uncategorized\
**Created:** [May 8, 2020, 10:41am UTC](https://www.ruby-forum.com/t/keep-getting-main-object-nameerror/259515 "2020-05-08T10:41:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibz95](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@ibz95](https://www.ruby-forum.com/u/ibz95)\
**Post date:** [May 8, 2020, 10:41am UTC](https://www.ruby-forum.com/t/keep-getting-main-object-nameerror/259515/1 "2020-05-08T10:41:57Z")

</div>

I want to write a code in Ruby that reads in information from a file and prints it out as an array in the terminal. This is what I have so far:

class Track  
attr\_accessor :name, :location

```
def initialize (name, location)
	@name = name
	@location = location
end

```

end

def read\_tracks(music\_file)

count = music\_file.gets().to\_i()  
i=0  
tracks = Array.new()  
while (i\<count)  
track = read\_track(music\_file)  
tracks \<\< track  
i+=1  
end

return tracks  
end

def read\_track(a\_file)  
track\_name=a\_file.gets.chomp  
track\_location = a\_file.gets.chomp  
track = Track.new(track\_name, track\_location)  
track.name= track\_name  
track.location= track\_location  
return track

end

def print\_tracks(tracks)  
index = 0  
while (index\<tracks.length)  
print\_track tracks[index]  
index+=1  
end  
end

def print\_track(track)  
puts('Track title is: ’ + track.title)  
puts('Track file location is: ’ + track.location)  
end

def main()  
a\_file = File.new(“input.txt”, “r”) # open for reading

print\_tracks(tracks)  
end

main()

But I keep getting the following error: Traceback (most recent call last):  
1: from track\_file\_handling.rb:68:in `<main>' track_file_handling.rb:65:in `main’: undefined local variable or method  
`tracks’ for main:Object (NameError)  
I am relatively new to ruby so I cannot figure out why this is happening. Any feedback on whether my code can actually do what I want it to will also be appreciated.

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 8, 2020, 2:23pm UTC](https://www.ruby-forum.com/t/keep-getting-main-object-nameerror/259515/2 "2020-05-08T14:23:43Z")

</div>

Read through your ‘main’ method:

```auto
def main()
  a_file = File.new(“input.txt”, “r”) # open for reading

  print_tracks(tracks)
end

```

It first opens a file to read, and names it `a_file`.  
Then it calls `print_tracks` passing it the value of variable `tracks`. But `tracks` is not defined, hence your error.

I suspect you are missing a call to `read_tracks` …

---

<div class="post-metadata">

**Author:** ![ibz95](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@ibz95](https://www.ruby-forum.com/u/ibz95)\
**Post date:** [May 8, 2020, 4:30pm UTC](https://www.ruby-forum.com/t/keep-getting-main-object-nameerror/259515/3 "2020-05-08T16:30:50Z")

</div>

Hi pcl. Thank you for your reply. I was able to get my code running thanks to your suggestion. However, it now printing out the output twice instead of once. For example. it is printing out:  
Crackling Rose  
sounds/01-Cracklin-rose.wav  
Soolaimon  
sounds/06-Soolaimon.wav  
Sweet Caroline  
sounds/20-Sweet\_Caroline.wav  
Crackling Rose  
sounds/01-Cracklin-rose.wav  
Soolaimon  
sounds/06-Soolaimon.wav  
Sweet Caroline  
sounds/20-Sweet\_Caroline.wav

instead of just:  
Crackling Rose  
sounds/01-Cracklin-rose.wav  
Soolaimon  
sounds/06-Soolaimon.wav  
Sweet Caroline  
sounds/20-Sweet\_Caroline.wav

Why might this be?

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 8, 2020, 5:10pm UTC](https://www.ruby-forum.com/t/keep-getting-main-object-nameerror/259515/4 "2020-05-08T17:10:34Z")

</div>

> However, it now printing out the output twice instead of once.

I don’t see any reason - it looks like `print_tracks` is being called twice. Take a look at `main` and see where/how it is printing.

Incidentally:

1. in your `read_track` method, you don’t have to set the values for the track name and location - you have passed them to the constructor, and the initialize method stores them into fields. It is simpler to write:

```auto
def read_track(a_file)
  track_name=a_file.gets.chomp
  track_location = a_file.gets.chomp
  return Track.new(track_name, track_location)
end

```

1. in `print_track` you first try to print `track.title` → this should be `track.name`.
