From: Pat Maddox Date: 2006-05-16T00:17:27+09:00 Subject: Weird class-loading issue (or maybe a problem with define_method?) I'm working on an app and having a strange class loading problem. I've extracted it to the most basic code possible. # foo.rb require "bar" class Foo FOO = "foo" end # bar.rb require "foo" class Bar def self.set_foo(val) define_method(:foo) { val } end end class FooBar < Bar set_foo Foo::FOO end This works fine if I include bar.rb first: baggio:~/work/test pergesu$ irb irb(main):001:0> require "bar" => true but it blows up if I do foo.rb: irb(main):001:0> require "foo" NameError: uninitialized constant FooBar::Foo from ./bar.rb:10 from ./foo.rb:1 from (irb):1 What's even more strange to me is that the FooBar class has no problem accessing Foo::FOO from within a method, either class or instance: class FooBar < Bar def foo; Foo::FOO; end def self.class_foo; Foo::FOO; end end irb(main):001:0> require "foo" => true irb(main):002:0> FooBar.new.foo => "foo" irb(main):003:0> FooBar.class_foo => "foo" I've also moved self.class_foo into the Bar superclass, and there were no problems. The only issue that pops up then is when I write a class method in Bar that ends up calling define_method passing in a constant from the other file. It only happens when I require "foo", but require "bar" works fine. Can anyone tell me what's going on here? I really don't understand it. Also I've foundt that my program works if I require "bar" before everything...but I don't like that solution. Most classes don't even know that that file ever gets required, so it feels ugly that I'd have to require it at the beginning of every file I use. Pat