Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bug in autoload_paths? #6350

Closed
NielsKSchjoedt opened this Issue · 6 comments

3 participants

NielsKSchjoedt Steve Klabnik Mark McSpadden
NielsKSchjoedt

I am encountering a strange bug in my code. I have a rails application with the following two files in the lib:

lib/module_one/module_two/class_one.rb

    module ModuleOne
      module ModuleTwo
        class ClassOne
          class << self
            def test
              puts 'Class one'
              ClassTwo.test
            end
          end
        end
      end
    end

and

lib/module_one/module_two/class_two.rb

    module ModuleOne
      module ModuleTwo
        class ClassTwo
          def self.test
            puts 'Class two'
          end
        end
      end
    end

Now my problem is, that when I go into the console and write:

ModuleOne::ModuleTwo::ClassOne.test

it throws the following: NameError: uninitialized constant ClassTwo

The strange thing is, that the problem seems to be connected to the use of class << self instead of self.method. If I change the class_one.rb file like this it works!:

    module ModuleOne
      module ModuleTwo
        class ClassOne
          def self.test
            puts 'Class one'
            ClassTwo.test
          end
        end
      end
    end

Im loading the files in application.rb like this:

config.autoload_paths += %W(#{config.root}/lib)

Is this a bug in rails, or is it just me getting something all wrong?

Im using rails 3.1.3 btw

Steve Klabnik
Collaborator

In class_one.rb your module is named Moduletwo, and in class_two.rb it's named moduletwo. Typo?

NielsKSchjoedt

Thanks - corrected! It was just a typo in the example - the bug is still there ;-)

NielsKSchjoedt

BTW The problem is also existing if you generate a complete new application with the two files, so it's nothing in my specific configuration...

Mark McSpadden

Confirmed the issue. Made a branch with few changes to the fixtures, a new test, and a hacky "solution" (not PR worthy): https://github.com/markmcspadden/rails/compare/issue_6350

@NielsKSchjoedt - This failing is due to the self change that happens in class << self. Doing def self.test does not change selfand therefore plays much nicer with the ActiveSupport const_missing implementation.

A writeup by @wycats is still one of the best references around on the subject: http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/

NielsKSchjoedt

Thanks. I posted the question on SO as well, and got a pretty good answer... Might be of interest http://stackoverflow.com/questions/10618952/bug-in-rails-autoload-paths/10633531#10633531

Steve Klabnik
Collaborator

So apparently this isn't actually a bug, so I'm closing.

Autoloading is hard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.