# Circular 'require'

**URL:** https://rubytalk.org/t/circular-require/47871
**Category:** ruby-talk
**Created:** [23 July 2008 09:48 UTC](https://rubytalk.org/t/circular-require/47871 "2008-07-23T09:48:13Z")
**Posts on this page:** 9
**Page:** 2

<div class="post-metadata">

### Author: ![Shadowfirebird](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Shadowfirebird](https://rubytalk.org/u/Shadowfirebird)
#### Post date: [24 July 2008 08:14 UTC](https://rubytalk.org/t/circular-require/47871/21 "2008-07-24T08:14:59Z")

</div>

::slaps heel of hand to forehead::

The END{ require } thing isn't needed. Setting $" solves the infinite  
loop -- you can just call require.

> **···**
>
> On Thu, Jul 24, 2008 at 9:02 AM, Shadowfirebird \<shadowfirebird@gmail.com\> wrote:
> 
> > I've had my think.
> > 
> > I think I have some conclusions which might be useful for other people  
> > in the same boat, and since I've not seen them anywhere else, I hope  
> > you'll allow me to bore you with them. No doubt cooler heads than  
> > mine will see things differently, but there you go. And, maybe I'm  
> > missing the obvious, anyway.
> > 
> > 1) require is mildly broken (a). It doesn't set $" until after the  
> > given file is loaded. This can lead to errors when you have a  
> > circular call of requires. If it set $" first, circular references  
> > would just be ignored. Of course, you should never actually \*need\* to  
> > have circular 'require' referefences, but: see my next point.
> > 
> > 2) Require is mildly broken (b). Secondly -- and I suppose that this  
> > is less of a bug and more of a bugbear -- there is no way to  
> > distinguish between require-because-it-wont-load-otherwise and  
> > require-because-some-methods-wont-run-otherwise. This second sort  
> > isn't entirely necessary, but it's aesthetically pleasing and useful  
> > for unit testing.
> > 
> > 3) You can get around (1) by manually pushing the program name into $"  
> > at the start of each file. This done, you can use a new idiom -- END  
> > { require } -- to specify  
> > require-because-some-methods-wont-run-otherwise. Not the prettiest  
> > thing, but neither the ugliest, I think: "in the end, this is  
> > required."
> > 
> > Now I suppose I'd better show my working if I'm going to say anything  
> > as contentious as all that. Apologies for the length of this, but  
> > then, you can stop reading now. OTOH, I'd really like to hear other  
> > points of view.
> > 
> > A new set of example programs:
> > 
> > # runner.rb  
> > require 'root'  
> > Root.create\_data()  
> > Root.customers.each{|c| puts c.oid, c.name}
> > 
> > # root.rb  
> > require 'customer'  
> > class Root  
> > &nbsp;&nbsp;@@list =
> > 
> > &nbsp;&nbsp;def self.create\_data()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Dolly")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Reymond")  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def self.customers()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @@list.find\_all{|x| x.kind\_of?(Customer)}  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;attr\_accessor :oid
> > 
> > &nbsp;&nbsp;def initialize()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@oid = @@list.size  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@list \<\< self  
> > &nbsp;&nbsp;end  
> > end
> > 
> > # customer.rb  
> > require 'root'  
> > class Customer \< Root  
> > &nbsp;&nbsp;attr\_accessor :name
> > 
> > &nbsp;&nbsp;def initialize(name)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@name = name  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Broadly speaking this follows the same approach as my actual code. As  
> > listed, of course, it doesn't work: run runner.rb and it loads  
> > root.rb, which starts by loading customer.rb, which starts by loading  
> > root.rb, which...
> > 
> > I needed to make a distinction (#2 above) between code that referred  
> > to another object when it was loaded; and code that referred to  
> > another object when it was run. root.rb doesn't need a 'require  
> > 'customer'; runner.rb does. But this is rather unhelpful when it  
> > comes to unit testing: when writing the test class for Root I  
> > shouldn't have to remember to require customer. Ideally I should be  
> > able to put the require in root.rb somehow.
> > 
> > For experimental purposes lets try putting the "require 'customer'" at  
> > the \*end\* of root.rb. This has practical implications -- no  
> > programmer is going to look for it there -- but it works; running  
> > runner.rb produces no errors. Oddly enough, putting "END {require  
> > 'customer'}" at the start of the file does not work. But, wait.
> > 
> > What is more interesting, is that if you run root.rb or customer.rb  
> > you get warnings about redefined methods. You shouldn't see this if  
> > require works as described -- a file should never be called twice.  
> > What appears to be happening is that $" is set after the file is  
> > loaded -- that is, it's not being set until the chain of requires has  
> > finished. We can defeat that behaviour by adding $" \<\< "\<filename\>"  
> > to the start of root.rb and customer.rb. The warnings now disappear.  
> > (And that seems like a bug to me, or at least an undocumented feature.)
> > 
> > This is really odd: if we now try END{ require 'customer'} at the  
> > start of root.rb -- well, after the $" thing -- it \*works\*. Really  
> > odd, but nice.
> > 
> > So, root.rb now looks like this:
> > 
> > # root.rb  
> > $" \<\< "root.rb"  
> > END { require 'customer' }  
> > class Root  
> > &nbsp;&nbsp;@@list =
> > 
> > &nbsp;&nbsp;def self.create\_data()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Dolly")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Reymond")  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def self.customers()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @@list.find\_all{|x| x.kind\_of?(Customer)}  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;attr\_accessor :oid
> > 
> > &nbsp;&nbsp;def initialize()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@oid = @@list.size  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@list \<\< self  
> > &nbsp;&nbsp;end  
> > end
> > 
> > And I can live with that. But I'm open to suggestions -- any  
> > suggestions, except rude ones...
> 
> --  
> Me, I imagine places that I have never seen / The colored lights in  
> fountains, blue and green / And I imagine places that I will never go  
> / Behind these clouds that hang here dark and low  
> But it's there when I'm holding you / There when I'm sleeping too /  
> There when there's nothing left of me / Hanging out behind the  
> burned-out factories / Out of reach but leading me / Into the  
> beautiful sea

---

<div class="post-metadata">

### Author: ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)
#### Post date: [24 July 2008 10:39 UTC](https://rubytalk.org/t/circular-require/47871/22 "2008-07-24T10:39:07Z")

</div>

Shadowfirebird wrote:

> # root.rb  
> require 'customer'  
> class Root  
> @@list =
> 
> def self.create\_data()  
> Customer.new("Dolly")  
> Customer.new("Reymond")  
> end
> 
> def self.customers()  
> return @@list.find\_all{|x| x.kind\_of?(Customer)}  
> end
> 
> attr\_accessor :oid
> 
> def initialize()  
> @oid = @@list.size  
> @@list \<\< self  
> end  
> end
> 
> # customer.rb  
> require 'root'  
> class Customer \< Root  
> attr\_accessor :name
> 
> def initialize(name)  
> super()  
> @name = name  
> end  
> end

Just leave out the require 'root' and the whole thing will work. You're making  
things out to be way more complicated than they are. There is no need to  
require the file that requires you.

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

### Author: ![Calamitas](https://avatars.discourse-cdn.com/v4/letter/c/b782af/32.png) [@Calamitas](https://rubytalk.org/u/Calamitas)
#### Post date: [24 July 2008 12:19 UTC](https://rubytalk.org/t/circular-require/47871/23 "2008-07-24T12:19:32Z")

</div>

Require isn't broken. Essentially, you could say that require has its  
internal version of $" that is set before executing a file (although  
as Stefano pointed out, it is maintained as the difference with the  
externally visible $").

Require works as advertised. But "ruby test1.rb" doesn't do a require  
of test1.rb, "ruby -r test1.rb" does that. "ruby test1.rb" simply  
executes the file without looking at or setting $", which is what  
#load does too. This is by design. If you explicitly set $" in  
test1.rb, you essentially give a load of that file some of the  
semantics of a require.

Peter

> **···**
>
> On Thu, Jul 24, 2008 at 10:02 AM, Shadowfirebird \<shadowfirebird@gmail.com\> wrote:
> 
> > 1) require is mildly broken (a). It doesn't set $" until after the  
> > given file is loaded. This can lead to errors when you have a  
> > circular call of requires. If it set $" first, circular references  
> > would just be ignored. Of course, you should never actually \*need\* to  
> > have circular 'require' referefences, but: see my next point.

---

<div class="post-metadata">

### Author: ![Shadowfirebird](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Shadowfirebird](https://rubytalk.org/u/Shadowfirebird)
#### Post date: [24 July 2008 08:33 UTC](https://rubytalk.org/t/circular-require/47871/24 "2008-07-24T08:33:28Z")

</div>

I take that back. But I don't understand why it's not true!

> **···**
>
> On Thu, Jul 24, 2008 at 9:14 AM, Shadowfirebird \<shadowfirebird@gmail.com\> wrote:
> 
> > ::slaps heel of hand to forehead::
> > 
> > The END{ require } thing isn't needed. Setting $" solves the infinite  
> > loop -- you can just call require.
> > 
> > On Thu, Jul 24, 2008 at 9:02 AM, Shadowfirebird \> \<shadowfirebird@gmail.com\> wrote:
> > 
> > > I've had my think.
> > > 
> > > I think I have some conclusions which might be useful for other people  
> > > in the same boat, and since I've not seen them anywhere else, I hope  
> > > you'll allow me to bore you with them. No doubt cooler heads than  
> > > mine will see things differently, but there you go. And, maybe I'm  
> > > missing the obvious, anyway.
> > > 
> > > 1) require is mildly broken (a). It doesn't set $" until after the  
> > > given file is loaded. This can lead to errors when you have a  
> > > circular call of requires. If it set $" first, circular references  
> > > would just be ignored. Of course, you should never actually \*need\* to  
> > > have circular 'require' referefences, but: see my next point.
> > > 
> > > 2) Require is mildly broken (b). Secondly -- and I suppose that this  
> > > is less of a bug and more of a bugbear -- there is no way to  
> > > distinguish between require-because-it-wont-load-otherwise and  
> > > require-because-some-methods-wont-run-otherwise. This second sort  
> > > isn't entirely necessary, but it's aesthetically pleasing and useful  
> > > for unit testing.
> > > 
> > > 3) You can get around (1) by manually pushing the program name into $"  
> > > at the start of each file. This done, you can use a new idiom -- END  
> > > { require } -- to specify  
> > > require-because-some-methods-wont-run-otherwise. Not the prettiest  
> > > thing, but neither the ugliest, I think: "in the end, this is  
> > > required."
> > > 
> > > Now I suppose I'd better show my working if I'm going to say anything  
> > > as contentious as all that. Apologies for the length of this, but  
> > > then, you can stop reading now. OTOH, I'd really like to hear other  
> > > points of view.
> > > 
> > > A new set of example programs:
> > > 
> > > # runner.rb  
> > > require 'root'  
> > > Root.create\_data()  
> > > Root.customers.each{|c| puts c.oid, c.name}
> > > 
> > > # root.rb  
> > > require 'customer'  
> > > class Root  
> > > &nbsp;&nbsp;@@list =
> > > 
> > > &nbsp;&nbsp;def self.create\_data()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Dolly")  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Reymond")  
> > > &nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;def self.customers()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @@list.find\_all{|x| x.kind\_of?(Customer)}  
> > > &nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;attr\_accessor :oid
> > > 
> > > &nbsp;&nbsp;def initialize()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@oid = @@list.size  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@list \<\< self  
> > > &nbsp;&nbsp;end  
> > > end
> > > 
> > > # customer.rb  
> > > require 'root'  
> > > class Customer \< Root  
> > > &nbsp;&nbsp;attr\_accessor :name
> > > 
> > > &nbsp;&nbsp;def initialize(name)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@name = name  
> > > &nbsp;&nbsp;end  
> > > end
> > > 
> > > Broadly speaking this follows the same approach as my actual code. As  
> > > listed, of course, it doesn't work: run runner.rb and it loads  
> > > root.rb, which starts by loading customer.rb, which starts by loading  
> > > root.rb, which...
> > > 
> > > I needed to make a distinction (#2 above) between code that referred  
> > > to another object when it was loaded; and code that referred to  
> > > another object when it was run. root.rb doesn't need a 'require  
> > > 'customer'; runner.rb does. But this is rather unhelpful when it  
> > > comes to unit testing: when writing the test class for Root I  
> > > shouldn't have to remember to require customer. Ideally I should be  
> > > able to put the require in root.rb somehow.
> > > 
> > > For experimental purposes lets try putting the "require 'customer'" at  
> > > the \*end\* of root.rb. This has practical implications -- no  
> > > programmer is going to look for it there -- but it works; running  
> > > runner.rb produces no errors. Oddly enough, putting "END {require  
> > > 'customer'}" at the start of the file does not work. But, wait.
> > > 
> > > What is more interesting, is that if you run root.rb or customer.rb  
> > > you get warnings about redefined methods. You shouldn't see this if  
> > > require works as described -- a file should never be called twice.  
> > > What appears to be happening is that $" is set after the file is  
> > > loaded -- that is, it's not being set until the chain of requires has  
> > > finished. We can defeat that behaviour by adding $" \<\< "\<filename\>"  
> > > to the start of root.rb and customer.rb. The warnings now disappear.  
> > > (And that seems like a bug to me, or at least an undocumented feature.)
> > > 
> > > This is really odd: if we now try END{ require 'customer'} at the  
> > > start of root.rb -- well, after the $" thing -- it \*works\*. Really  
> > > odd, but nice.
> > > 
> > > So, root.rb now looks like this:
> > > 
> > > # root.rb  
> > > $" \<\< "root.rb"  
> > > END { require 'customer' }  
> > > class Root  
> > > &nbsp;&nbsp;@@list =
> > > 
> > > &nbsp;&nbsp;def self.create\_data()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Dolly")  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Reymond")  
> > > &nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;def self.customers()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @@list.find\_all{|x| x.kind\_of?(Customer)}  
> > > &nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;attr\_accessor :oid
> > > 
> > > &nbsp;&nbsp;def initialize()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@oid = @@list.size  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@list \<\< self  
> > > &nbsp;&nbsp;end  
> > > end
> > > 
> > > And I can live with that. But I'm open to suggestions -- any  
> > > suggestions, except rude ones...
> > 
> > --  
> > Me, I imagine places that I have never seen / The colored lights in  
> > fountains, blue and green / And I imagine places that I will never go  
> > / Behind these clouds that hang here dark and low  
> > But it's there when I'm holding you / There when I'm sleeping too /  
> > There when there's nothing left of me / Hanging out behind the  
> > burned-out factories / Out of reach but leading me / Into the  
> > beautiful sea
> 
> --  
> Me, I imagine places that I have never seen / The colored lights in  
> fountains, blue and green / And I imagine places that I will never go  
> / Behind these clouds that hang here dark and low  
> But it's there when I'm holding you / There when I'm sleeping too /  
> There when there's nothing left of me / Hanging out behind the  
> burned-out factories / Out of reach but leading me / Into the  
> beautiful sea

---

<div class="post-metadata">

### Author: ![Pit\_Capitain](https://avatars.discourse-cdn.com/v4/letter/p/d78d45/32.png) [@Pit\_Capitain](https://rubytalk.org/u/Pit_Capitain)
#### Post date: [24 July 2008 11:00 UTC](https://rubytalk.org/t/circular-require/47871/25 "2008-07-24T11:00:04Z")

</div>

Like the OP, I like to require files that are needed to execute the  
current file, in order to not become dependent on the exact require  
sequence. So another solution to the problem of his would be to place  
the require calls at the appropriate places, for example in root.rb:

&nbsp;&nbsp;class Root

&nbsp;&nbsp;&nbsp;&nbsp;require 'customer'  
&nbsp;&nbsp;&nbsp;&nbsp;def self.create\_data()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Dolly")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Reymond")  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;end

But instead of reading and trying to understand what others have  
posted he keeps complaining about require being broken. I don't see  
anything in the documentation of require that doesn't behave as  
advertised.

Regards,  
Pit

> **···**
>
> 2008/7/24 Sebastian Hungerecker \<sepp2k@googlemail.com\>:
> 
> > Just leave out the require 'root' and the whole thing will work. You're making  
> > things out to be way more complicated than they are. There is no need to  
> > require the file that requires you.

---

<div class="post-metadata">

### Author: ![Shadowfirebird](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Shadowfirebird](https://rubytalk.org/u/Shadowfirebird)
#### Post date: [24 July 2008 12:41 UTC](https://rubytalk.org/t/circular-require/47871/26 "2008-07-24T12:41:13Z")

</div>

Ah! Thank you! That's brilliant!

> **···**
>
> On Thu, Jul 24, 2008 at 1:19 PM, Calamitas \<calamitates@gmail.com\> wrote:
> 
> > On Thu, Jul 24, 2008 at 10:02 AM, Shadowfirebird \> \<shadowfirebird@gmail.com\> wrote:
> > 
> > > 1) require is mildly broken (a). It doesn't set $" until after the  
> > > given file is loaded. This can lead to errors when you have a  
> > > circular call of requires. If it set $" first, circular references  
> > > would just be ignored. Of course, you should never actually \*need\* to  
> > > have circular 'require' referefences, but: see my next point.
> > 
> > Require isn't broken. Essentially, you could say that require has its  
> > internal version of $" that is set before executing a file (although  
> > as Stefano pointed out, it is maintained as the difference with the  
> > externally visible $").
> > 
> > Require works as advertised. But "ruby test1.rb" doesn't do a require  
> > of test1.rb, "ruby -r test1.rb" does that. "ruby test1.rb" simply  
> > executes the file without looking at or setting $", which is what  
> > #load does too. This is by design. If you explicitly set $" in  
> > test1.rb, you essentially give a load of that file some of the  
> > semantics of a require.
> > 
> > Peter
> 
> --  
> Me, I imagine places that I have never seen / The colored lights in  
> fountains, blue and green / And I imagine places that I will never go  
> / Behind these clouds that hang here dark and low  
> But it's there when I'm holding you / There when I'm sleeping too /  
> There when there's nothing left of me / Hanging out behind the  
> burned-out factories / Out of reach but leading me / Into the  
> beautiful sea

---

<div class="post-metadata">

### Author: ![Shadowfirebird](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Shadowfirebird](https://rubytalk.org/u/Shadowfirebird)
#### Post date: [24 July 2008 11:31 UTC](https://rubytalk.org/t/circular-require/47871/27 "2008-07-24T11:31:01Z")

</div>

I apologise if I seem to be complaining. That's not it. I found a  
problem in what I was doing, worked out a way around it, and --partly  
since it involved a "gotcha" that I'd never seen explained anywhere  
else, partly because I'm still learning -- I posted it here to see if  
anyone had any comment, had a better idea.

Pit, I think your solution is interesting because it imbeds a class  
definition inside a class. Are there any downsides to that?

> **···**
>
> On Thu, Jul 24, 2008 at 12:00 PM, Pit Capitain \<pit.capitain@gmail.com\> wrote:
> 
> > 2008/7/24 Sebastian Hungerecker \<sepp2k@googlemail.com\>:
> > 
> > > Just leave out the require 'root' and the whole thing will work. You're making  
> > > things out to be way more complicated than they are. There is no need to  
> > > require the file that requires you.
> > 
> > Like the OP, I like to require files that are needed to execute the  
> > current file, in order to not become dependent on the exact require  
> > sequence. So another solution to the problem of his would be to place  
> > the require calls at the appropriate places, for example in root.rb:
> > 
> > class Root
> > 
> > &nbsp;&nbsp;&nbsp;require 'customer'  
> > &nbsp;&nbsp;&nbsp;def self.create\_data()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Dolly")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer.new("Reymond")  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > end
> > 
> > But instead of reading and trying to understand what others have  
> > posted he keeps complaining about require being broken. I don't see  
> > anything in the documentation of require that doesn't behave as  
> > advertised.
> > 
> > Regards,  
> > Pit
> 
> --  
> Me, I imagine places that I have never seen / The colored lights in  
> fountains, blue and green / And I imagine places that I will never go  
> / Behind these clouds that hang here dark and low  
> But it's there when I'm holding you / There when I'm sleeping too /  
> There when there's nothing left of me / Hanging out behind the  
> burned-out factories / Out of reach but leading me / Into the  
> beautiful sea

---

<div class="post-metadata">

### Author: ![Pit\_Capitain](https://avatars.discourse-cdn.com/v4/letter/p/d78d45/32.png) [@Pit\_Capitain](https://rubytalk.org/u/Pit_Capitain)
#### Post date: [24 July 2008 14:23 UTC](https://rubytalk.org/t/circular-require/47871/28 "2008-07-24T14:23:45Z")

</div>

> I apologise if I seem to be complaining.

No need to apologize, everybody should have the right to complain. I  
thought that you either hadn't read or tried what I wrote in my first  
mail and therefore got the (in my view) false impression that require  
was broken. That's the reason for my maybe too harsh an answer, sorry.

> Pit, I think your solution is interesting because it imbeds a class  
> definition inside a class. Are there any downsides to that?

Calling require inside a class definition or even inside a method  
doesn't embed the loaded constants (class names are constants too)  
into the "calling" class. It just changes the time when require is  
called. In your case, it would mean that require is called \*after\*  
creating the class Root, so that the required file can use that name.

Sometimes I've seen code that requires certain files only if some  
method is called which needs the required functionality:

&nbsp;&nbsp;def method\_with\_special\_requirements  
&nbsp;&nbsp;&nbsp;&nbsp;require "rarely\_used\_library"  
&nbsp;&nbsp;&nbsp;&nbsp;use\_the\_required\_functionality  
&nbsp;&nbsp;end

This avoids the overhead of loading all the files everytime, even if  
the funcationality isn't used at all.

Regards,  
Pit

> **···**
>
> 2008/7/24 Shadowfirebird \<shadowfirebird@gmail.com\>:

---

<div class="post-metadata">

### Author: ![Shadowfirebird](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Shadowfirebird](https://rubytalk.org/u/Shadowfirebird)
#### Post date: [24 July 2008 19:19 UTC](https://rubytalk.org/t/circular-require/47871/29 "2008-07-24T19:19:47Z")

</div>

Pit, your idiom of putting require inside the class definition appears  
to be considerably more robust than my half-baked notions.

> **···**
>
> On Thu, Jul 24, 2008 at 3:23 PM, Pit Capitain \<pit.capitain@gmail.com\> wrote:
> 
> > 2008/7/24 Shadowfirebird \<shadowfirebird@gmail.com\>:
> > 
> > > I apologise if I seem to be complaining.
> > 
> > No need to apologize, everybody should have the right to complain. I  
> > thought that you either hadn't read or tried what I wrote in my first  
> > mail and therefore got the (in my view) false impression that require  
> > was broken. That's the reason for my maybe too harsh an answer, sorry.
> > 
> > > Pit, I think your solution is interesting because it imbeds a class  
> > > definition inside a class. Are there any downsides to that?
> > 
> > Calling require inside a class definition or even inside a method  
> > doesn't embed the loaded constants (class names are constants too)  
> > into the "calling" class. It just changes the time when require is  
> > called. In your case, it would mean that require is called \*after\*  
> > creating the class Root, so that the required file can use that name.
> > 
> > Sometimes I've seen code that requires certain files only if some  
> > method is called which needs the required functionality:
> > 
> > def method\_with\_special\_requirements  
> > &nbsp;&nbsp;&nbsp;require "rarely\_used\_library"  
> > &nbsp;&nbsp;&nbsp;use\_the\_required\_functionality  
> > end
> > 
> > This avoids the overhead of loading all the files everytime, even if  
> > the funcationality isn't used at all.
> > 
> > Regards,  
> > Pit
> 
> --  
> Me, I imagine places that I have never seen / The colored lights in  
> fountains, blue and green / And I imagine places that I will never go  
> / Behind these clouds that hang here dark and low  
> But it's there when I'm holding you / There when I'm sleeping too /  
> There when there's nothing left of me / Hanging out behind the  
> burned-out factories / Out of reach but leading me / Into the  
> beautiful sea

[Previous page](https://rubytalk.org/t/circular-require/47871.md?page=1)
