Forum: Ruby Newb - uninitialized constant error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F23f0f30a19ec89e0ecd5e02a7935dbe?d=identicon&s=25 Steven R. (Guest)
on 2007-02-13 23:46
(Received via mailing list)
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
962613f3d9e4306aada868261feb3cf1?d=identicon&s=25 unknown (Guest)
on 2007-02-13 23:56
(Received via mailing list)
On Feb 13, 2:41 pm, "Steven R." <steverum...@comcast.net> 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
F23f0f30a19ec89e0ecd5e02a7935dbe?d=identicon&s=25 Steven R. (Guest)
on 2007-02-14 00:01
(Received via mailing list)
coachhilton@gmail.com 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])[\/.](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
Cd0b2459b5cf38b2ab78ab5de544559e?d=identicon&s=25 Todd Werth (twerth)
on 2007-02-14 06:12
Put all that in a method:


def your_method()

  ...

  doc1 = TextDocumentPortrait.new("Document 1")
  ...

end
This topic is locked and can not be replied to.