# Newb - uninitialized constant error

**URL:** <https://www.ruby-forum.com/t/newb-uninitialized-constant-error/85482>\
**Category:** Ruby\
**Created:** [February 13, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/newb-uninitialized-constant-error/85482 "2007-02-13T23:46:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![srogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@srogers](https://www.ruby-forum.com/u/srogers)\
**Post date:** [February 13, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/newb-uninitialized-constant-error/85482/1 "2007-02-13T23:46:07Z")

</div>

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])/.[/.]\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

---

<div class="post-metadata">

**Author:** ![srogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@srogers](https://www.ruby-forum.com/u/srogers)\
**Post date:** [February 13, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/newb-uninitialized-constant-error/85482/2 "2007-02-13T23:56:09Z")

</div>

On Feb 13, 2:41 pm, “Steven R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Many thanks.  
> class Main  
> for count in 0…10
> 
> 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

---

<div class="post-metadata">

**Author:** ![srogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@srogers](https://www.ruby-forum.com/u/srogers)\
**Post date:** [February 14, 2007, 12:01am UTC](https://www.ruby-forum.com/t/newb-uninitialized-constant-error/85482/3 "2007-02-14T00:01:09Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > I am working out of the ‘Pickaxe’ book, first edition, as a reference.
> > 
> > …

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])/.[/.]\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

---

<div class="post-metadata">

**Author:** ![srogers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@srogers](https://www.ruby-forum.com/u/srogers)\
**Post date:** [February 14, 2007, 6:12am UTC](https://www.ruby-forum.com/t/newb-uninitialized-constant-error/85482/4 "2007-02-14T06:12:32Z")

</div>

Put all that in a method:

def your\_method()

…

doc1 = TextDocumentPortrait.new(“Document 1”)  
…

end
