Search code examples
rubyargv

Troubles with argv :uninitialized constant ARVG (NameError)


I am working through a tutorial in which I have built a text analyzer. At this point it is built and working as expected and I am now supposed to include a summarizer which will find the most relevant sentences to summarize the text. That is also built and working as expected. It is in a separate file, as specified. My problem is getting ARVG to read the files. I also have some confusion as to how the summarize.rb file is included. This the first time doing this so please correct me where my logic and/or code is wrong.

I have 3 files:

1) text.txt -> which holds the original text

2) summarize.rb -> holds the program to analyze the text in text.txt

3) ARGV.rb -> to run the text through summarize??

The instructions state I should be writing, in the command line

ruby analyzer.rb text.txt

to which I receive an error:

analyzer.rb:5:in `<main>': uninitialized constant ARVG (NameError)

the instructions say nothing about needing to initialize anything concerning running the arvg file.

here is what is in my analyzer.rb file

File.open("text.txt") 

lines = File.readlines(ARVG[0]) 
line_count = lines.size #this counts the lines in the new array
text = lines.join 
total_characters = text.length
total_characters_nonspace = text.gsub(/\s/, '').length
word_count = text.split.length
total_sentences = text.split(/\.|\?|!/).length
paragraphs = text.split(/\n\n/).count 
words_per_sentence = word_count / total_sentences
sentences_per_paragraph = total_sentences / paragraphs

stop_words = %w(the by a on the for of are with just but and to the my I has some in)


#puts text
puts
puts "stats:"
puts "#{line_count} lines"
puts "#{total_characters} total characters"
puts "#{total_characters_nonspace} total characters not including spaces"
puts "#{word_count} is the word count"
puts "#{total_sentences} is the number of sentences"
puts "#{paragraphs} total paragraphs"
puts
puts "Averages:"
puts "#{words_per_sentence} average words per sentence"
puts "#{sentences_per_paragraph} average sentences per paragraph"
puts 

Here is what is in my summerize.rb file - it may be worth noting that in the lesson, the text to be summarized has hardcoded in the file and attached to a variable. Also none of this was wrapped as a method. I made it a method that takes a argument (whatever text) because it made more sense to make it reusable with any text rather what was hard-coded in the file. The instructions never specified to do that. It just left the hardcoded text in. Could the changes be a problem?

def summarizer (str)
  sentences = str.gsub(/\s+/, ' ').strip.split(/\.|\?|!/) 
  sentences_sorted = sentences.sort_by{|sentence| sentence.length} 
  one_third = sentences_sorted.length / 3 
  ideal_sentences = sentences_sorted.slice(one_third, one_third + 1) 
  ideal_sentences = ideal_sentences.select{|sentence| sentence =~ /is|are/} 
  puts ideal_sentences.join('. ')
end

Here is what summarize.rb looked like before I changed it from what the lesson specified.

text = %q{Ruby is a great programming language. It is object oriented and has many groovey features. Some people don't like it but that's not our problem! It's easy to learn.It's great. To learn more about Ruby visit the official Ruby website today}

  sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|!/) #getting rid of white space and splitting @ end of sentence
  sentences_sorted = sentences.sort_by{|sentence| sentence.length} #sorts sentences by length
  one_third = sentences_sorted.length / 3 #calculates what a third of sentences are since we don't want too many.
  ideal_sentences = sentences_sorted.slice(one_third, one_third + 1) #slices out the first and last third
  ideal_sentences = ideal_sentences.select{|sentence| sentence =~ /is|are/} #<= defines words we are looking for
  puts ideal_sentences.join('. ')

And here is what is in my ARGV.rb file

puts ARGV.join('-')

as instructed.

My questions are, why isn't this working...and how is the text.txt file being run through summarize.rb (which is what I think is supposed to be happening)when summarize.rb is never called by ARGV.rb nor text.txt? And this is exactly how the instructions indicated I set this up (I've read them at least five times in case I missed something...)


Solution

  • analyzer.rb:5:in `<main>': uninitialized constant ARVG (NameError)
    

    This message is telling you that there is a problem on line 5 of your program.

    Which I'm assuming is this line:

    lines = File.readlines(ARVG[0]) 
    

    It's telling you that you haven't initialised a constant called ARVG... which is correct - you are using a constant called ARVG ... but I'm assuming what you meant to use was the constant ARGV (note spelling mistake)?