'require' problem

Hi guys!

This file, testrequire.rb , has the following code:

require ‘string_extensions’
puts “This is a test”.vowels.join(’-’)

This file, string_extensions.rb , has the following code:

class String
def vowels
self.scan(/[aeiou]/i)
end
end

I run testrequire in Command Prompt, I get:
internal:lib/rubygems/custom_require:29:in require': no such file to load -- string_extensions (LoadError) from <internal:lib/rubygems/custom_require>:29:inrequire’
from O:/Ruby/Practice/testrequire.rb:1:in `’

Help please!

Thanks =)

you need

require_relative ‘string_extensions’

and it is not an bug with require

If you’re not on 1.9.2 and don’t have require_relative, I believe this
will work:

require ‘./string_extensions’

Good luck! :slight_smile:

Sam R. wrote in post #1030126:
If you’re not on 1.9.2 and don’t have require_relative, I believe this
require ‘./string_extensions’

The problem with doing something like this is that the requiring file
will only find the required file when the program’s invoked from that
directory. For example, if you move back one directory and then try to
run it, it won’t work.

It’s a good idea to make scripts as portable as possible, so something
like this would be a good idea, I think.

require File.join(File.expand_path(File.dirname(FILE)),
‘string_extensions’)

What this means:
FILE is a string of the current filename
File.dirname(f) gets the directory of f
File.expand_path(f) expands things like “~” and “.”
File.join(*files) joins all the files with whatever file separating
character is appropriate on your system. Ex: ‘/’, ‘’

As much as this might seem like overkill, it might be a good habit
to get into because LoadErrors are annoying as hell.

And yeah, on Ruby >= 1.9, require_relative does all this for you.

-Luke

Hi guys! Yes, I’m using 1.9.2 .

Is there another way that would allow me to just use ‘require’ ?

The tutorial I’m reading doesn’t say anything about ‘require_relative’.

Thanks

On Nov 4, 2011, at 23:02 , Kaye Ng wrote:

Hi guys! Yes, I’m using 1.9.2 .

Is there another way that would allow me to just use ‘require’ ?

ruby -I. your_script.rb

Hey I got it.
I just put this code at the start:

$:.push ‘O:/Ruby/Practice’

thanks!

Ryan D. wrote in post #1030259:

ruby -I. your_script.rb

Thanks Ryan, but like I said, I would like to use just require
‘my_script’

=)

On 11/05/2011 12:23 AM, Kaye Ng wrote:

Ryan D. wrote in post #1030259:

ruby -I. your_script.rb
Thanks Ryan, but like I said, I would like to use just require
‘my_script’

=)

Not sure why the previous suggestions are not sufficient, but you can
add

$LOAD_PATH.unshift “.”

prior to using ‘require’

-Justin

Justin C. wrote in post #1030715:

On 11/05/2011 12:23 AM, Kaye Ng wrote:

Ryan D. wrote in post #1030259:

ruby -I. your_script.rb
Thanks Ryan, but like I said, I would like to use just require
‘my_script’

=)

Not sure why the previous suggestions are not sufficient, but you can
add

$LOAD_PATH.unshift “.”

prior to using ‘require’

-Justin

but manipulating LOAD_PATH only to use require again instat of
require_relative is an VERY BIG security risk

Try the following 2 commands:

ruby -e “puts $:”
ruby -I. -e “puts $:”

You will see that the 2nd command lists your current directory as one of
its load paths.

-Jingjing

The tutorial I’m reading doesn’t say anything about ‘require_relative’.

then your tutorial is to old for your ruby version

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