using 'require'

P

Peter Bailey

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
 
R

Raj Sahae

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"
 
T

Tim Hunter

Peter said:
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
 
P

Peter Bailey

Raj said:
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!
 
R

Raj Sahae

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
 
B

Brian Candler

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)
 
R

rogerdpack

you could try
require "./BNAbudgetperiods"

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

Roger Pack

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
 
J

Jeff

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

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
 
C

Chris Carter

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.caboo.se/53939
 
J

John Joyce

on my PPC Mac and on the Dreamhost Debian Linux servers,
is in the loadpath, it comes first, but only appears as . so
you might not see it at first!
That's a common linux and OSX is a common BSD so I'd assume it may be
a windows issue.
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.caboo.se/53939
 
B

bbiker

on my PPC Mac and on the Dreamhost Debian Linux servers,
is in the loadpath, it comes first, but only appears as . so
you might not see it at first!
That's a common linux and OSX is a common BSD so I'd assume it may be
a windows issue.
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", "."]

- Show quoted text -

This is not a window issue.

C:\Documents and Settings\Owner>ruby -e "p $:"
["c:/ruby/lib/ruby/site_ruby/1.8",
"c:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt",
"c:/ruby/lib/ruby/site_ruby",
"c:/ruby/lib/ruby/1.8",
"c:/ruby/lib/ruby/1.8/i386-mswin32",
"."]
 

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,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top