Circular 'require'

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

On Thu, Jul 24, 2008 at 9:14 AM, Shadowfirebird
[email protected] wrote:

I think I have some conclusions which might be useful for other people

require-because-some-methods-wont-run-otherwise. Not the prettiest

def self.create_data()
def initialize()
def initialize(name)

programmer is going to look for it there – but it works; running
(And that seems like a bug to me, or at least an undocumented feature.)
class Root

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

Shadowfirebird wrote:

def self.customers()

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

2008/7/24 Sebastian H. [email protected]:

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

require 'customer'
def self.create_data()
  Customer.new("Dolly")
  Customer.new("Reymond")
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

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 C. [email protected]
wrote:

class Root
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

On Thu, Jul 24, 2008 at 10:02 AM, Shadowfirebird
[email protected] 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

Ah! Thank you! That’s brilliant!

On Thu, Jul 24, 2008 at 1:19 PM, Calamitas [email protected]
wrote:

as Stefano pointed out, it is maintained as the difference with the


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

2008/7/24 Shadowfirebird [email protected]:

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
require “rarely_used_library”
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

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 C. [email protected]
wrote:

require “rarely_used_library”
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

On Wednesday 23 July 2008, Alex G. wrote:

Also, I don’t think Stefano’s explanation can be quite right - if it
went into an infinite loop as he describes you would never see an
error message, it would just never end…

You’re perfectly right. I don’t know what I was thinking of when I wrote
that
mail (actually, I do. I read the subject line and didn’t pay enough
attention
to the message body. Then, I didn’t even think to check whether things
were
indeed going as I thought). Forget my answer completely, and sorry for
giving
a wrong information.

Stefano