Help? Unitialized constant NameError in Model

Hello,

I am totally stumped on this one.

I have two classes sitting in the lib folder under the main application
directory. For now, those two classes are identical except for the file
name (attribute.rb and group.rb) and the name of the classes Attribute
and Group respectively.

In the models directory I have a model that contains two instance
variables. One will hold an array of Attribute objects and the other
will contain an array of Group objects. (Note that the class doesn’t
extent ActiveRecord::Base because I don’t need a DB right now.

That said, I can instantiate Attribute objects all day long and push
them into the array. But it blows up when I try to create an instance
of a Group object telling me that there is an unitialized constant
Prod::Group (NameError)

Anyone have any idea wth I’m doing wrong? I don’t use a require
statement for either file because it’s in the lib directory and is
unneeded

The two classes in the lib directory look like this for now:

class Attribute
attr_accessor :name
attr_accessor :value

def initialize(name, value)
@name = name
@value = value
end
end

It’s really not anything complicated so I don’t have a clue how one
blows up and the other doesnt!!!

David Sainte-claire wrote:

It’s really not anything complicated so I don’t have a clue how one
blows up and the other doesnt!!!

This must be something else to this puzzle. I created your setup on an
new Rails application and tested in the console. Here are the results:

$ ./script/console
Loading development environment (Rails 2.3.4)

att = Attribute.new(“Hello”, “World”)
=> #<Attribute:0x1005da970 @name=“Hello”, @value=“World”>

gr = Group.new(“Hello”, “World”)
=> #<Group:0x10059da48 @name=“Hello”, @value=“World”>

att.name
=> “Hello”

att.value
=> “World”

gr.name
=> “Hello”

gr.value
=> “World”

And the classes:

class Attribute
attr_accessor :name
attr_accessor :value

def initialize(name, value)
@name = name
@value = value
end
end

class Group
attr_accessor :name
attr_accessor :value

def initialize(name, value)
@name = name
@value = value
end
end

Robert W. wrote:

David Sainte-claire wrote:

It’s really not anything complicated so I don’t have a clue how one
blows up and the other doesnt!!!

This must be something else to this puzzle. I created your setup on an
new Rails application and tested in the console. Here are the results:

$ ./script/console
Loading development environment (Rails 2.3.4)

att = Attribute.new(“Hello”, “World”)
=> #<Attribute:0x1005da970 @name=“Hello”, @value=“World”>

gr = Group.new(“Hello”, “World”)
=> #<Group:0x10059da48 @name=“Hello”, @value=“World”>

att.name
=> “Hello”

att.value
=> “World”

gr.name
=> “Hello”

gr.value
=> “World”

And the classes:

class Attribute
attr_accessor :name
attr_accessor :value

def initialize(name, value)
@name = name
@value = value
end
end

class Group
attr_accessor :name
attr_accessor :value

def initialize(name, value)
@name = name
@value = value
end
end

The probability of this happening is almost nil, but of course it had to
happen to me! When I finally resolved it I had nearly pulled all my
hair out. I was using REXML to parse an XML document and since I am new
to Ruby I did require ‘rexml’ and include ‘REXML’ which imported the
REXML namespace. What I found out later is that REXML also has an
Attribute class within it, that has attributes with :name and :value,
and a constructor that takes :name, and :value as arguments in the same
order and produces the same behaviour. So my class wasn’t being used at
all, I just didn’t know it!

I found it out by putting in a debug puts #(Attribute} and found that it
was of type REXML/Attribute.

I can’t believe how long I spent on that before I found it. Seriously,
that was one for the record books!

Lesson to other new programmers to Ruby, don’t include things that will
obscure your namespace until you’re sure that you’re not stepping on
something! It took me two days before I figured out that the code I was
using wasn’t even the code I wrote, because it was working exactly the
same!