From: "Steven R." Date: 2007-02-14T08:00:05+09:00 Subject: Re: Newb - uninitialized constant error coachhilton@gmail.com wrote: > On Feb 13, 2:41 pm, "Steven R." wrote: >> I am a Newbie who has written some Ruby code - it actually works when >> 'all in one file'. To make it more re-usable and practice good coding >> style, I broke it into 2 - one for classes, one for test code. Call them >> 'Textractor.rb' and 'main.rb'. >> >> I've accepted that it is a simple error, but I can't track it down. I am >> posting so someone who has not stared at the code as long as I have may >> see right away what is evading me. >> >> I am working out of the 'Pickaxe' book, first edition, as a reference. >> >> I tried Google-ing the error message, but no dice. >> >> I also tried renaming the classes to include reference to the >> Textractor.rb. BTW - Textractor.rb is three classes wrapped up in >> 'module Textractor'. >> >> If I omitted any key info. needed to diagnose the problem, it's probably >> because I didn't recognize it as such, which in turn is probably where I >> went wrong. >> >> Many thanks. >> >> I keep getting the following error: >> >> main.rb:59: uninitialized constant Main::TextElement (NameError) from >> main.rb:56 >> >> Now, TextElement is a properly named class in 'Textractor.rb'. >> >> My code is as follows: >> >> class Main >> require 'Textractor.rb' >> >> # Testing Logic Follows >> dateElement = >> Regexp.new(/(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.]\d\d/) >> # Create a new document >> doc1 = TextDocumentPortrait.new("Document 1") >> >> THE LINE 56 BELOW GENERATES THE ERROR...: >> >> for count in 0...10 >> >> # Create a new line, element and add the element to the line >> newLine = TextLinePortrait.new("Line " + count.to_s) >> >> ...IN REFERENCE TO THE FOLLOWING LINE 59: >> >> newElement = TextElement.new("Date", dateElement, "R") >> newLine.addElement(newElement) >> >> ... >> >> Code continues > > I think we'd need to see all the code to be sure about the problem. > But since you mention that your code is wrapped in a module, you may > need to qualify references to its members, as in: > > newElement = Textractor::TextElement.new("Date", dateElement, "R") > ^^^^^^^^^^ > Ken > Thanks, Ken - I tried your solution, but I got: main.rb:55: uninitialized constant Textractor::TextDocumentPortrait (NameError) Here's the listing of main.rb, including your fix: class Main require 'Textractor.rb' # Testing Logic Follows dateElement = Regexp.new(/(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.]\d\d/) # Create a new document LINE 55 FOLLOWS: doc1 = Textractor::TextDocumentPortrait.new("Document 1") for count in 0...10 # Create a new line, element and add the element to the line newLine = Textractor::TextLinePortrait.new("Line " + count.to_s) newElement = Textractor::TextElement.new("Date", dateElement, "R") newLine.addElement(newElement) # Add the line to the DocumentPortrait doc1.addLine(newLine) end doc1.printLines() # End testing logic end