The -r option uses two C functions, add_modules and require_libraries
in ruby.c.
add_modules adds each argument of -r to internal req_list
then eventually require_libraries loads them all in series.
ubygems.rb redefines Kernel#require method whose implementation is
rb_f_require in eval.c.
It does not affect how -r works.
btw, RUBYOPT is evaluated *after* parsing options (so -T can forbit it)
Can anyone explain to me why, with RUBYOPT="-rubgems"
The difference is not whether you provide -rubygems on the command line
or in an environment variable. The difference is whether you use -ransi
or -e'require "ansi"'
Note that none of these work:
$ ruby -rbundler -e 'puts "hello"'
ruby: no such file to load -- bundler (LoadError)
$ ruby -rubygems -rbundler -e 'puts "hello"'
ruby: no such file to load -- bundler (LoadError)
$ env RUBYOPT="-rubygems" ruby -rbundler -e 'puts "hello"'
ruby: no such file to load -- bundler (LoadError)
But it works this way if you have -rubygems:
$ ruby -e 'require "bundler"; puts "hello"'
-e:1:in `require': no such file to load -- bundler (LoadError)
from -e:1
$ ruby -rubygems -e 'require "bundler"; puts "hello"'
hello
$ env RUBYOPT="-rubygems" ruby -e 'require "bundler"; puts "hello"'hello
hello
This is with ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
I imagine it's because rubygems redefines Kernel#require, but using -r
on the command line calls the internal require function directly. If -r
doesn't do a full method dispatch, then rubygems can't override what it
does.
Ah, just like autoload. Thank you for figuring this out.
I suppose I will report this on redmine. I don't see how it can be
construed as anything but broken behavior. but then again I've only
brought up the problem with autoload for, oh I don't know, something
like FIVE YEARS to no avail.
btw, my original usecase, that led me this, was an attempt to run
minitest-based tests through a custom report format.
$ ruby -r'minitest/tapy' test/test-*.rb
that I can't do this is ANNOYING AS HELL, to say the least.
···
On Oct 7, 5:11 am, Brian Candler <b.cand...@pobox.com> wrote:
I imagine it's because rubygems redefines Kernel#require, but using -r
on the command line calls the internal require function directly. If -r
doesn't do a full method dispatch, then rubygems can't override what it
does.
I suppose I will report this on redmine. I don't see how it can be
construed as anything but broken behavior. but then again I've only
brought up the problem with autoload for, oh I don't know, something
like FIVE YEARS to no avail.
It has come up before and been rejected. You might not want to waste your time.
btw, my original usecase, that led me this, was an attempt to run
minitest-based tests through a custom report format.
$ ruby -r'minitest/tapy' test/test-*.rb
IIRC, the TestTask in rake allows you to add extra commandline options. I use my own from hoe, so I'm not that familiar with it.
Yes, I have been informed that with 1.9.x, rubygems is always loaded
so RUBYOPT="-rubygems" is no longer even needed. That fixes the issue
for loading from gems. It remains however a limitation for any custom
load manager, like the one I use.
A simpler example of the limitation would be load monitor --overriding
require to log everything request.
$ cat req.rb
puts "Custom Require"
module Kernel
alias :require0 :require
def require(*a)
puts "Kernel#require"
p a
require0(*a)
end
class << self
alias :require0 :require
def require(*a)
puts "Kernel.require"
p a
require0(*a)
end
end
end
Now if we do:
$ export RUBYOPT="-rreq.rb"
Then the difference between:
ruby -rhaml -e'p Haml.constants'
and
ruby -e'require "haml"; p Haml.constants'
is apparent.
···
On Oct 8, 5:24 am, Brian Candler <b.cand...@pobox.com> wrote:
Thomas Sawyer wrote in post #1025570:
> btw, my original usecase, that led me this, was an attempt to run
> minitest-based tests through a custom report format.
> $ ruby -r'minitest/tapy' test/test-*.rb
And the problem is that this doesn't work because rubygems isn't
installed, or something else?
Well at first I was going to have `minitap/tapy` defined with:
MiniTest::Unit.runner = MiniTest::TapY.new
So one could, for example, run tests and get the TAP-Y output via:
$ ruby -rminitest/tapy test/some-test.rb
During development I use my custom load manger (Roll), rather then
Rubygems, so it could not find 'minitest/tapy' due to forementioned
problem. But since then I decided that this wasn't a good way to
handle things for minitap, so that particular use is no longer
important. Nonetheless it made it clear to me that my load manager had
a problematic edge case due to -r implementation and the order RUBYOPT
is applied.
···
On Oct 8, 5:13 pm, Brian Candler <b.cand...@pobox.com> wrote:
Thomas Sawyer wrote in post #1025738:
> Then the difference between:
> ruby -rhaml -e'p Haml.constants'
> and
> ruby -e'require "haml"; p Haml.constants'
> is apparent.
Indeed. But is it a problem for minitest/tapy? If so, why?