Don't understand -r

Can anyone explain to me why, with RUBYOPT="-rubgems", this works:

$ ruby -e'require "ansi"; "x".ansi(:red)'

But,

$ ruby -ransi -e'"x".ansi(:red)'

gives an error ruby: no such file to load -- ansi (LoadError). Even
if I do:

$ ruby -rubygems -ransi -e'"x".ansi(:red)'

it still doesn’t work.

On Fri, Oct 7, 2011 at 3:05 AM, Intransition [email protected]
wrote:

$ ruby -rubygems -ransi -e’“x”.ansi(:red)’

it still doesn’t work.

What happens for

$ ruby -r rubygems -r ansi -e ‘“x”.ansi(:red)’

And what version btw.?

Cheers

robert

2011/10/7 Intransition [email protected]:

$ ruby -ransi -e’“x”.ansi(:red)’
$ ruby -rubygems -ransi -e’“x”.ansi(:red)’

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)


OZAWA Sakuro

“I think we can agree, the past is over.” - George W. Bush

Thomas S. wrote in post #1025489:

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.

On Oct 7, 5:11am, Brian C. [email protected] 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.

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, 2:48am, Robert K. [email protected] wrote:

And what version btw.?

I tried on 1.8.7 and 1.9.3

On Oct 7, 3:20am, OZAWA Sakuro [email protected] wrote:

btw, RUBYOPT is evaluated after parsing options (so -T can forbit it)

hmm… if -T is the only reason for this, then -T should be
preprocessed.

On Oct 7, 2011, at 05:30 , Intransition wrote:

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.

Thomas S. 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?

For me, -r works with rubygems in 1.9.2:

$ sudo gem192 install haml
Successfully installed haml-3.1.3

$ ruby192 -rhaml -e ‘p Haml.constants’
[:ROOT_DIR, :Util, :Version, :VERSION, :Helpers, :Buffer, :Shared,
:Parser, :Compiler, :Filters, :Error, :SyntaxError, :Engine]

$ ruby192 -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]

Thomas S. 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?

On Oct 8, 5:13pm, Brian C. [email protected] wrote:

is apparent.

Indeed. But is it a problem for minitest/tapy? If so, why?

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

For me, -r works with rubygems in 1.9.2:

$ sudo gem192 install haml
Successfully installed haml-3.1.3

$ ruby192 -rhaml -e ‘p Haml.constants’
[:ROOT_DIR, :Util, :Version, :VERSION, :Helpers, :Buffer, :Shared,
:Parser, :Compiler, :Filters, :Error, :SyntaxError, :Engine]

$ ruby192 -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs