简体   繁体   中英

Ruby uninitialized constant NameError

I have two ruby files. The first is

require 'www/poster'

poster = Poster.new()

The second ('www/poster.rb') is

require ...

class Poster ... end

The problem is, when I run the first file, I get an error:

in `<main>': uninitialized constant Poster (NameError)

But if I replace the original code with just

require ...
class Poster

end

__END__
class Poster #original class here

it's ok. The code worked fine in Ruby 1.8.6, problems started after upgrading to 1.9.1p129 (2009-05-12 revision 23412) [i386-mswin32]. Is there any workaround?

There is absolutely nothing whatsoever in your code that would cause it to behave differently in Ruby 1.9 than in Ruby 1.8. And indeed, when I copy & paste the code you posted into two files, they work exactly like the are supposed to, in all of the following configurations:

  • MRI Ruby 1.8.6 (2009-03-31 patchlevel 368) [i386-mingw32]
  • YARV Ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32]
  • JRuby 1.4.0dev (ruby 1.8.7p174) (2009-08-24 6586) (Java HotSpot(TM) Client VM 1.6.0_14-ea) [x86-java]
  • JRuby 1.4.0dev (ruby 1.9.1p0) (2009-08-24 6586) (Java HotSpot(TM) Client VM 1.6.0_14-ea) [x86-java]

I got this error

`<class:Employee>': uninitialized constant Employee::Datamapper (NameError)

when compiling. Here is the code

class Employee
  include Datamapper::Resource
  property :id,             Serial
....

The reason for the error was I spelled DataMapper with a lowercase 'm'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM