'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:in `require'
        from O:/Ruby/Practice/testrequire.rb:1:in `<main>'

Help please!

Thanks =)

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

you need

require_relative 'string_extensions'

and it is not an bug with require

···

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

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

$:.push 'O:/Ruby/Practice'

thanks!

···

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

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

then your tutorial is to old for your ruby version

···

--
Posted via http://www.ruby-forum.com/\.

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:

···

On 4 November 2011 10:11, Hans Mackowiak <hanmac@gmx.de> wrote:

you need

require_relative 'string_extensions'

and it is not an bug with require

--
Posted via http://www.ruby-forum.com/\.

Sam Rose 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.

-Luke

···

--
Posted via http://www.ruby-forum.com/\.

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

···

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

ruby -I. your_script.rb

···

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' ?

Ryan Davis wrote in post #1030259:

ruby -I. your_script.rb

Thanks Ryan, but like I said, I would like to use just *require
'my_script'*

=)

···

--
Posted via http://www.ruby-forum.com/\.

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

   $LOAD_PATH.unshift "."

prior to using 'require'

-Justin

···

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

Ryan Davis wrote in post #1030259:

ruby -I. your_script.rb

Thanks Ryan, but like I said, I would like to use just *require
'my_script'*

=)

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

···

-----Original Message-----
From: Kaye Ng [mailto:sbstn26@yahoo.com]
Sent: Saturday, November 05, 2011 12:23 AM
To: ruby-talk ML
Subject: Re: 'require' problem

Ryan Davis wrote in post #1030259:

ruby -I. your_script.rb

Thanks Ryan, but like I said, I would like to use just *require
'my_script'*

=)

--
Posted via http://www.ruby-forum.com/\.

Justin Collins wrote in post #1030715:

···

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

Ryan Davis 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

--
Posted via http://www.ruby-forum.com/\.