require behavior change from 1.8.7 p299 to 1.9.2 p0

B

Bharat Ruparel

requiring a file is causing problems for me in Ruby 1.9.2 p0. This
issue did not happen in Ruby 1.8.7 p299.

Here is a ruby file hello_world.rb

=E2=86=92 cat hello_world.rb

require 'sinatra'

get '/' do
"Hello world #{params[:name]}".strip
end

In Ruby 1.8.7, I get into irb and type the following:

=E2=86=92 rvm use ruby-1.8.7-p299

info: Using ruby 1.8.7 p299

bruparel:~/temp/sin_test
=E2=86=92 irb
ruby-1.8.7-p299 > require 'rubygems'
=3D> true
ruby-1.8.7-p299 > require 'hello_world'
=3D> true

Now I switch to Ruby 1.9.2 as follows:

bruparel:~/temp/sin_test
=E2=86=92 rvm use ruby-1.9.2-p0

info: Using ruby 1.9.2 p0

bruparel:~/temp/sin_test
=E2=86=92 irb
ruby-1.9.2-p0 > require 'rubygems'
=3D> true
ruby-1.9.2-p0 > require 'hello_world'
LoadError: no such file to load -- hello_world
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from (irb):2
from /home/bruparel/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'

However, if I issue a slightly different require command at irb prompt
as shown below:

ruby-1.9.2-p0 > require './hello_world'
=3D> true

Then it works!

But this is causing all the "old" Ruby 1.8.x programs to break. Is
there an environment setting in Ruby 1.9.2 that will restore the old
behavior?

Bharat

-- =

Posted via http://www.ruby-forum.com/.=
 
L

Luis Lavena

requiring a file is causing problems for me in Ruby 1.9.2 p0.  This
issue did not happen in Ruby 1.8.7 p299.

http://www.ruby-lang.org/en/news/2010/08/18/ruby-1-9-2-is-released/

"$: no longer includes the current directory."

That means "." is not included and thus you can't require things in
the same folder (is a security measure)
info: Using ruby 1.9.2 p0

bruparel:~/temp/sin_test
→ irb
ruby-1.9.2-p0 > require 'rubygems'
 => true
ruby-1.9.2-p0 > require 'hello_world'
LoadError: no such file to load -- hello_world
  from <internal:lib/rubygems/custom_require>:29:in `require'
  from <internal:lib/rubygems/custom_require>:29:in `require'
  from (irb):2
  from /home/bruparel/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'

However, if I issue a slightly different require command at irb prompt
as shown below:

ruby-1.9.2-p0 > require './hello_world'
 => true

Then it works!

But this is causing all the "old" Ruby 1.8.x programs to break.  Is
there an environment setting in Ruby 1.9.2 that will restore the old
behavior?

severals:

ruby -I. my_script.rb

require_relative 'hello_world'

or play with $: adding the current directory which I would recommend
not to.
 
B

Bharat Ruparel

Thanks Luis. I did some reading on Sinatra/rack-testing plus Ruby 1.9.2
upgrade. I was really trying to get tests to work in my Sinatra
application. I was successful in being able to run the tests. Here is
how:

I have a file called sharpener.rb in my project base directory. I
created a test directory and a file called test_shortener.rb in it.
Here is a snippet of require statements in it:

require 'rubygems'
require 'sinatra'
require 'test/unit'
require 'rack/test'

require File.join(Dir.pwd, 'shortener')

ENV['RACK_ENV'] = 'test'

class TestShortener < Test::Unit::TestCase

....

end

Note that I am running the tests from the base directory as follows:

ruby test/test_shortener.rb

It works fine.

I am curious though. How is including the current directory in the path
a security risk?

Appreciate your time.

Bharat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top