Odd edge case with require

$ cat test.rb
require 'test.rb’
puts “Should this only be outputted once?”

$ ruby test.rb
Should this only be outputted once?
Should this only be outputted once?

$ ruby -v
ruby 1.6.7 (2002-03-01) [i586-mswin32]

The above behaviour seems a bit strange to me.

~ Patrick May

Hi,

From: “Patrick May” patrick-may@monmouth.com
Sent: Tuesday, October 08, 2002 4:33 PM

$ cat test.rb
require ‘test.rb’
puts “Should this only be outputted once?”

$ ruby test.rb
Should this only be outputted once?
Should this only be outputted once?

$ ruby -v
ruby 1.6.7 (2002-03-01) [i586-mswin32]

‘require’ only checks duplicated requiring.

Try;
ruby -rtest.rb -e 0

The above behaviour seems a bit strange to me.

Could be, although it wouldn’t matter.
Can ruby check this? Or should not check?

Regards,
// NaHi

$ cat test.rb
require ‘test.rb’
puts “Should this only be outputted once?”

$ ruby test.rb
Should this only be outputted once?
Should this only be outputted once?

$ ruby -v
ruby 1.6.7 (2002-03-01) [i586-mswin32]

The above behaviour seems a bit strange to me.

~ Patrick May

Fair enough, but it doesn’t seem strange to me. I can’t explain why not, but
perhaps there can be no self-consistent system that handles this bounday case.
Not of great import, anyway.

Gavin

···

From: “Patrick May” patrick-may@monmouth.com

Hi –

···

On Tue, 8 Oct 2002, Patrick May wrote:

$ cat test.rb
require ‘test.rb’
puts “Should this only be outputted once?”

$ ruby test.rb
Should this only be outputted once?
Should this only be outputted once?

$ ruby -v
ruby 1.6.7 (2002-03-01) [i586-mswin32]

The above behaviour seems a bit strange to me.

Try it with load instead of require :slight_smile: (infinite loop)

I don’t find the above strange. It only require’s once, but
the puts statement gets encountered twice. That corresponds
to the behavior: no infinite loop, two lines of output.

David


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

Hi everyone,

Note that this issue was also raised in [ruby-talk:34467]

dblack@candle.superlink.net wrote in message news:Pine.LNX.4.44.0210081139100.13060-100000@candle.superlink.net

Try it with load instead of require :slight_smile: (infinite loop)

I don’t find the above strange. It only require’s once, but
the puts statement gets encountered twice. That corresponds
to the behavior: no infinite loop, two lines of output.

Maybe I can express my discomfort a different way:

Should the entry script be considered one of the $LOADED_FEATURES?

~ Patrick

“Gavin Sinclair” gsinclair@soyabean.com.au wrote in message news:0d4201c26edd$e7bc4ba0$526332d2@nosedog

perhaps there can be no self-consistent system that handles this bounday case.

The fix for this is not complicated. At some point, an array $" /
$LOADED_FEATURES is initialized. AFAIK, this state is used to
determine whether to require or not. I would like for the first entry
in that array to be initialized to $0 / $PROGRAM_NAME if applicable –
I’m sure this is < 10 LOC.

Not of great import, anyway.

This caused a problem for me, and also for [ruby-talk:34467]. Note
that in the past thread I didn’t consider this behavior an issue, but
now I do :slight_smile:

It’s hard to see how annoying this error is unless you’ve had to debug
it. When you are focused on the issue, it is a pretty straightforward
thing to understand. When you are combing through your application,
it is frustrating that an extra require statement can cause borked
behaviour, especially since it usually doesn’t.

I can’t imagine what would be broken by the fix outlined

~ Patrick

P.S. I know this is an impolite thing to do – the open source thing
to do is to have a patch with this thread. But I’m not comfortable
enough with my C to make such changes.