[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

using 'require'

Peter Bailey

3/2/2007 7:55:00 PM

A simple question . . .

I wrote a little Ruby script that defines budgetary periods during the
course of a year. This is for my company's budget schedule. There are 13
budget periods during any year. Anyway, I did this script. It seems to
work. I tested it and I can do a "put" of whatever budget period it is
now.

I'd like to use this code in other scripts. It's named
"BNAbudgetperiods.rb." So, I did a require 'BNAbudgetperiods' in one of
my other scripts. But, when I do a put in that second script, it just
gives me "nil." It doesn't complain. It just gives me "nil." Is there
something special I need to do in the hierarchy of Ruby script locations
so that other scripts can respect any other script I want to require?
These two scripts are in the same directory.

Thanks,
Peter

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

12 Answers

Raj Sahae

3/2/2007 8:25:00 PM

0


> Is there
> something special I need to do in the hierarchy of Ruby script locations
> so that other scripts can respect any other script I want to require?
> These two scripts are in the same directory
There are a couple ways to solve this problem. I don't know if there is
a best way. Hopefully someone with more experience can shed light on
that for me. But the problem you are having is that your scripts are
not in a directory that is included when ruby searches for the files
that you require. To solve this, I simply add the directory to the path
variable "$:".

So, if you are keeping scripts called myscript.rb and otherscript.rb in
a folder c:\myfolder, then in your script you would say

$: << "c:/myfolder/"
require "myscript"
require "otherscript"



Tim Hunter

3/2/2007 8:50:00 PM

0

Peter Bailey wrote:
> A simple question . . .
>
> I wrote a little Ruby script that defines budgetary periods during the
> course of a year. This is for my company's budget schedule. There are 13
> budget periods during any year. Anyway, I did this script. It seems to
> work. I tested it and I can do a "put" of whatever budget period it is
> now.
>
> I'd like to use this code in other scripts. It's named
> "BNAbudgetperiods.rb." So, I did a require 'BNAbudgetperiods' in one of
> my other scripts. But, when I do a put in that second script, it just
> gives me "nil." It doesn't complain. It just gives me "nil." Is there
> something special I need to do in the hierarchy of Ruby script locations
> so that other scripts can respect any other script I want to require?
> These two scripts are in the same directory.
>
> Thanks,
> Peter

Ruby searches for required files in a search path that is defined when
you install Ruby. You can see what directories are in the search path by
entering this command:

ruby -e"puts $:"

You can either put your script in a directory in the search path, or you
can modify the search path by adding directories to it. There are two
ways to modify the search path:

A) set the RUBYLIB environment variable to a list of colon-delimited
directories. Each directory in the list will be added to the front of
the search path.

export RUBYLIB=/home/me/my/scripts/dir
or
set RUBYLIB=C:\my\scripts\dir

B) use the -I option when you run your script. You can specify -I more
than once if necessary.

ruby -I my/scripts/dir myscript.rb

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

Peter Bailey

3/2/2007 9:12:00 PM

0

Raj Sahae wrote:
>> Is there
>> something special I need to do in the hierarchy of Ruby script locations
>> so that other scripts can respect any other script I want to require?
>> These two scripts are in the same directory
> There are a couple ways to solve this problem. I don't know if there is
> a best way. Hopefully someone with more experience can shed light on
> that for me. But the problem you are having is that your scripts are
> not in a directory that is included when ruby searches for the files
> that you require. To solve this, I simply add the directory to the path
> variable "$:".
>
> So, if you are keeping scripts called myscript.rb and otherscript.rb in
> a folder c:\myfolder, then in your script you would say
>
> $: << "c:/myfolder/"
> require "myscript"
> require "otherscript"

Raj,
I got an "unexpected tLSHFT" error message when I put that $: line at
the top. But, I appreciate your explanation. Using what you told me and
what Tim suggested next helped me to make it work. I put my "required"
Ruby file in ../ruby/lib/1.8, with all those other .rb files in there.
Thanks a lot!

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

Peter Bailey

3/2/2007 9:13:00 PM

0



That worked, Tim. Thanks. I just put my file into ../ruby/lib/1.8. Cool.

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

Raj Sahae

3/2/2007 9:24:00 PM

0


> Raj,
> I got an "unexpected tLSHFT" error message when I put that $: line at
> the top. But, I appreciate your explanation. Using what you told me and
> what Tim suggested next helped me to make it work. I put my "required"
> Ruby file in ../ruby/lib/1.8, with all those other .rb files in there.
> Thanks a lot!
>

I have no idea why that would happen. The exact excerpt that I use in
my code is:

$file_dir = './STFiles/'
$: << $file_dir

and that works fine. If you wanted just add the current directory, try
$: << "./"
and maybe that will work better.

Raj


Brian Candler

3/5/2007 7:37:00 PM

0

On Sat, Mar 03, 2007 at 06:11:52AM +0900, Peter Bailey wrote:
> Raj Sahae wrote:
> >> Is there
> >> something special I need to do in the hierarchy of Ruby script locations
> >> so that other scripts can respect any other script I want to require?
> >> These two scripts are in the same directory
> > There are a couple ways to solve this problem. I don't know if there is
> > a best way. Hopefully someone with more experience can shed light on
> > that for me. But the problem you are having is that your scripts are
> > not in a directory that is included when ruby searches for the files
> > that you require. To solve this, I simply add the directory to the path
> > variable "$:".
> >
> > So, if you are keeping scripts called myscript.rb and otherscript.rb in
> > a folder c:\myfolder, then in your script you would say
> >
> > $: << "c:/myfolder/"
> > require "myscript"
> > require "otherscript"
>
> Raj,
> I got an "unexpected tLSHFT" error message when I put that $: line at
> the top.

Try:

$:.unshift("c:/myfolder/")

(I don't see why the << syntax wouldn't work. unshift has the advantage of
putting your directory at the *front* of the library search path, which
allows you to override existing modules should you so wish)

Roger Pack

4/15/2007 1:52:00 AM

0

you could try
require "./BNAbudgetperiods"

If that works, I consider I consider it a ruby bug to not check the
current directory for libs.

On Mar 5, 1:37 pm, Brian Candler <B.Cand...@pobox.com> wrote:
> On Sat, Mar 03, 2007 at 06:11:52AM +0900, Peter Bailey wrote:
> > Raj Sahae wrote:
> > >> Is there
> > >> something special I need to do in the hierarchy of Ruby script locations
> > >> so that other scripts can respect any other script I want to require?
> > >> These two scripts are in the same directory
> > > There are a couple ways to solve this problem. I don't know if there is
> > > a best way. Hopefully someone with more experience can shed light on
> > > that for me. But the problem you are having is that your scripts are
> > > not in a directory that is included when ruby searches for the files
> > > that you require. To solve this, I simply add the directory to the path
> > > variable "$:".
>
> > > So, if you are keeping scripts called myscript.rb and otherscript.rb in
> > > a folder c:\myfolder, then in your script you would say
>
> > > $: << "c:/myfolder/"
> > > require "myscript"
> > > require "otherscript"
>
> > Raj,
> > I got an "unexpected tLSHFT" error message when I put that $: line at
> > the top.
>
> Try:
>
> $:.unshift("c:/myfolder/")
>
> (I don't see why the << syntax wouldn't work. unshift has the advantage of
> putting your directory at the *front* of the library search path, which
> allows you to override existing modules should you so wish)


Roger Pack

4/15/2007 2:09:00 AM

0

So this is my knowledge of it so far:



So it seems that if you require a ruby file that is in your local
directory, a la
require 'localDirectoryfile.rb'
this returns true, but doesnâ??t work. It loads the file and the class
names, but leaves out all of the functions, etc.

To get it to work

instead of

require â??localfileâ??

require â??./localfileâ?? works.

Go figure.

Also note there is a discrepancy between require and load, which is
interesting. load _requires_ the â??.rbâ? (at least for me) while require
does not. A minor inconsistency.

Any thots?
-Roger

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

Jeff

4/15/2007 2:10:00 AM

0

On Mar 2, 4:12 pm, Peter Bailey <pbai...@bna.com> wrote:
> That worked, Tim. Thanks. I just put my file into ../ruby/lib/1.8. Cool.
>
> --
> Posted viahttp://www.ruby-....

Matz, if you're out there -- could 1.9 change the default behavior of
'require' and 'load' to always include the current directory '.' in
the searchpath?

I ask because threads like this one seem to come up a lot, especially
for those of us new to Ruby, and it makes me think that it justifies a
change in how the searchpath works.

Thanks!
Jeff
softiesonrails.com


Chris Carter

4/15/2007 5:39:00 AM

0

On 4/14/07, Jeff <cohen.jeff@gmail.com> wrote:
> On Mar 2, 4:12 pm, Peter Bailey <pbai...@bna.com> wrote:
> > That worked, Tim. Thanks. I just put my file into ../ruby/lib/1.8. Cool.
> >
> > --
> > Posted viahttp://www.ruby-....
>
> Matz, if you're out there -- could 1.9 change the default behavior of
> 'require' and 'load' to always include the current directory '.' in
> the searchpath?
>
> I ask because threads like this one seem to come up a lot, especially
> for those of us new to Ruby, and it makes me think that it justifies a
> change in how the searchpath works.
>
> Thanks!
> Jeff
> softiesonrails.com
>
>
>

On my machine '.' is in the loadpath.

saurasaurusrex:~ cdcarter$ ruby -e "p $:"
["/usr/local/lib/ruby/site_ruby/1.8",
"/usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.7.1",
"/usr/local/lib/ruby/site_ruby", "/usr/local/lib/ruby/1.8",
"/usr/local/lib/ruby/1.8/i686-darwin8.7.1", "."]

also:
http://pastie.cabo...

--
Chris Carter
concentrationstudios.com
brynmawrcs.com