Require circularity?

I’m wondering if it is ever necessary to do something like this:

unless defined? SomeName then
require 'somename’
end

in case source files require each other. Thanks in advance,

  • Jim -

Hi –

I’m wondering if it is ever necessary to do something like this:

unless defined? SomeName then
require ‘somename’
end

‘require’ won’t reload, so you’re OK. ‘load’ will, however, so you
could end up with a loop there. But usually you use ‘load’ in cases
where you know that you want to reload at runtime. Most often you’d
use ‘require’, which should not cause a problem.

(Now, sit back and wait for reports of all the scenarios I haven’t
thought of… :slight_smile:

David

···

On Sat, 14 Dec 2002, James Davis wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

James Davis wrote:

I’m wondering if it is ever necessary to do something like this:

unless defined? SomeName then
require ‘somename’
end

in case source files require each other. Thanks in advance,

In general, no. Ruby is wa-ah-ay better than C and C++ .

‘require’ will only load in the file if it hasn’t already been loaded.

However, I think there was some thread a while back on a few
pathological cases where ‘require’ didn’t save the day. One was the case
of reaching the same .rb file from different paths (i.e. require
‘somename’ versus require ‘/usr/lib/ruby/somename’). I think there was
one more pathological case, but I can’t seem to remember it nor find it
on ruby-talk. But these are pathological, ergo they are only likely show
up when you’re getting too clever for your own good. :slight_smile:

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

‘require’ will only load in the file if it hasn’t already been loaded.

However, I think there was some thread a while back on a few
pathological cases where ‘require’ didn’t save the day. One was the case
of reaching the same .rb file from different paths (i.e. require
‘somename’ versus require ‘/usr/lib/ruby/somename’).
[…]

My test (ruby 1.6.5 (2001-09-19) [i386-cygwin] – I really must upgrade)
indicates that require is tricked by relative v. absolute path. I consider
this quite a serious bug, not just a pathological case.

Gavin

···

From: “Kent Dahl” kentda@stud.ntnu.no

“Gavin Sinclair” gsinclair@soyabean.com.au writes:

My test (ruby 1.6.5 (2001-09-19) [i386-cygwin] – I really must
upgrade) indicates that require is tricked by relative v. absolute
path. I consider this quite a serious bug, not just a pathological
case.

Maybe storing some kind of hash of require’d files could prevent this?

···


Josh Huber

“Gavin Sinclair” gsinclair@soyabean.com.au writes:

My test (ruby 1.6.5 (2001-09-19) [i386-cygwin] – I really must
upgrade) indicates that require is tricked by relative v. absolute
path. I consider this quite a serious bug, not just a pathological
case.

Maybe storing some kind of hash of require’d files could prevent this?

This is already the case (I believe it’s an array, though). The problem is
that obviously the “memory” of require’d files doesn’t store them all as
absolute paths. Maybe there’s a reason for this?

Gavin

···

From: “Josh Huber” huber@alum.wpi.edu