Forum: Ruby Mutual require problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7f0e84cd23b48a95579103261401605f?d=identicon&s=25 Renaud Delbru (rdelbru)
on 2006-03-02 18:50
Hi,

I have a problem with the require method.

First file 'main.rb':


> require 'test'
>
> class A
> 	def self.print
> 		p "test"
> 	end
> end
>
> Test.print

Second file 'test.rb':

> require 'main'
>
> class Test < A
> end

And the result during the execution :
workspace/test-divers/main.rb:10: uninitialized constant Test
(NameError)

It's a simple example to show you the problem, but in a more complex
program, I have the same problem.
I think I understand what happen,
ruby begin to load main.rb, see the require 'test',
try to load test.rb, see the require 'main',
try to load main.rb with success, but when he arrived to
Test.print, ruby doesn't finish to load 'test.rb' and Test
become a uninitialized constant.

Thanks if you have a solution or a proposition.

--
Renaud Delbru
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2006-03-02 20:55
(Received via mailing list)
On 3/2/06, renaud delbru <renaud.delbru@deri.org> wrote:
> >       def self.print
> > class Test < A
> > end
>
> And the result during the execution :
> workspace/test-divers/main.rb:10: uninitialized constant Test
> (NameError)
>


Circular requires are a bad thing.
See http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/....

To avoid them, try the ruby equivalent of a forward declaration: In
test.rb, replace
> require 'main'
with
> class A; end

-Adam
D111305c32e46f7dd2794a956208d347?d=identicon&s=25 E. Saynatkari (Guest)
on 2006-03-02 23:39
Adam Shelly wrote:
> On 3/2/06, renaud delbru <renaud.delbru@deri.org> wrote:
>> >       def self.print
>> > class Test < A
>> > end
>>
>> And the result during the execution :
>> workspace/test-divers/main.rb:10: uninitialized constant Test
>> (NameError)
>>
>
>
> Circular requires are a bad thing.
> See http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/....
>
> To avoid them, try the ruby equivalent of a forward declaration: In
> test.rb, replace
>> require 'main'
> with
>> class A; end

..or, if possible, try to not have both files referencing
eachother. It is *usually* not necessary.

> -Adam


E
7f0e84cd23b48a95579103261401605f?d=identicon&s=25 Guest (Guest)
on 2006-03-03 11:05
Thank you adam for your answers,

I have resolved the problem with a toplevel file which contains
the sequence of require.

Bye.

--
Renaud Delbru
2fce3b5a094cd28a0538bc4b314d1c09?d=identicon&s=25 Han Holl (Guest)
on 2006-03-03 13:28
(Received via mailing list)
Another useful technique here is autoloading.
There are cases where require is needed, but in general autoload is much
better.
Looser coupling, less memory use, faster programs.

Cheers,

Han Holl
This topic is locked and can not be replied to.