"Uninitialized Constant" Error When Including a Module

Uninitialized constant error when including a module

Andrew Marshall has an excellent point about the auto-load setup (see the question he links for more on that), but also: Because you named your class ListerExtension, Rails will be looking for a file named lister_extension.rb - not lister.rb. It's smart, but it's not that smart.

Hope that helps!

uninitialized constant (NameError) while including module

You try to use method as Formulas 'module method', while you defined it as regular instance method. So it should be called on RepaymentSchedule instance:

rs = RepaymentSchedule.new
rs.method

Also, you need to make sure your loading order is correct. Here, you should require file containing Formulas module before you load Schedule class, otherwise you get uninitialized constant error.

Ruby returns uninitialized constant error when trying to include a module within a chef recipe

Inserting the following line resolved this issue for me:

::Chef::Recipe.send(:include, Windows::Helper)

This allows me to use the variables following module from the windows cookbook:

module Windows
module Helper
...
{Variable}
{Other_variable}
...

Rails 7 controller decorator uninitialised constant error in production only

Problem here is that when lazy loading, nobody is referencing a constant called ...::BasePublicDecorator. However, Zeitwerk expects that constant to be defined in that file, and the mismatch is found when eager loading.

The solution is to configure the autoloader to ignore the decorators, because you are handling their loading, and because they do not define constants after their names. This documentation has an example. It needs to be adapted to your engine, but you'll see the idea.

For completeness, let me also explain that in Zeitwerk, eager loading is a recursive const_get, not a recursive require. This is to guarantee that if you access the constant, loading succeeds or fails consistently in both modes (and it is also a tad more efficient). Recursive const_get still issues require calls via Module#autoload, and if you ran one for some file idempotence also applies, but Zeitwerk detects the expected constant is not defined anyway, which is an error condition.

Ruby including Errors module in another module - uninitialized constant Errors

Non-rails apps doesn't have auto-load classes, so you need to manually require it.
Also one thing of your code, I don't know why you are using :: before constant, I don't see any naming-confusion in order to use it. It is used only when you have your Errors modules on different namespace, and you need to resolve the one from your namespace.

# lib_formatter
require 'errors'

module Formatter
extend Errors
# ...
end

The second issue with your code, that you have defined instance method, but where you include your code there class method (module method)

So errors class should be adjusted to

module Errors
# ...
module_function
# ...
end

The third issue is include/extend misusage.

Include used to make all methods from module you using to become instance methods.

Extend used to make all methods from module you using to become class methods.

include/extend info

ruby module NameError: uninitialized constant error

raise Errors::Configuration, "Msg94 auth key missing!" unless @authkey

Use fully qualified name to help ruby lookup the class.

raise ::Msg91sms::Errors::Configuration, "Msg94 auth key missing!" unless @authkey

The folder structure is like Errors::Configuration but the error is showing like Configuration::Errors..Dont know why..

It's trying to find Errors::Configuration within Msg91sms::Configuration (the current scope at that point). But since there's no Msg91sms::Configuration::Errors, it fails with that message.

NameError: uninitialized constant #Class:0x00007fadd32ea580::Searchable

This turns out to be caused by incompatibility with Byebug. Byebug stops the events Zeitwerk uses for autoloading to fire off in a debugging session. The issue is described here.

The << was a typo and would not result in that error.

uninitialized constant error after change style of children definitions at classes and modules

As a general rule, I'd advise against nested class/module definitions. Sure, it saves a line of code/indentation, but you run in to this sort of issue.

For example, see this rubocop rule:

Class: RuboCop::Cop::Style::ClassAndModuleChildren

Overview

This cop checks the style of children definitions at classes and
modules. Basically there are two different styles:

The compact style is only forced for classes/modules with one child.

Examples:

  • EnforcedStyle: nested (default)

    # good
    # have each child on its own line
    class Foo
    class Bar
    end
    end
  • EnforcedStyle: compact

    # good
    # combine definitions as much as possible
    class Foo::Bar
    end

Defining nested modules is problematic, as it only works in ruby if the parent module has already been defined. For example:

irb(main):001:1* module Foo::Bar
irb(main):002:0> end
Traceback (most recent call last):
1: from (irb):1
NameError (uninitialized constant Foo)

Whereas this works fine:

irb(main):001:1* module Foo
irb(main):002:2* module Bar
irb(main):003:1* end
irb(main):004:0> end

Additionally, even if the parent module has already been defined, you can still run into other issues with module nesting in ruby.

So, in short: module ConcurrentRails::Adapters::Future didn't work, because ConcurrentRails::Adapters hadn't been defined yet.

You could resolve this by explicitly defining that "parent module" first, however, I wouldn't advise 3+ layer nested module definitions regardless.



Related Topics



Leave a reply



Submit