I'm coming from a java world and I'm trying to understand the structure
of a Ruby project and how 'require's are resolved.
In java we have this nice package routine where we put our .java files
into a directory hierarchy that is nice and neat.
Can I do the same in Ruby (I assume yes) but if I do how are "requires"
resolved when needed and what happend if I move code around the
hierarchy; are the resolve paths relative or abosolute based on some
'root' directory I"m unaware of.
(Currently to learn and test all my .rb files are in one directory...
it's getting messy)
It's actually something like Java's classpath. There are several default locations to search, and paths can be relative to any of them - Ruby will pick the first one it finds.
You can set the paths to be searched in the environment variable RUBYLIB, or access/modify it within Ruby using the built-in variable $:
So require 'foo' will use the first 'foo.rb' it comes across in any of these directories.
You can, in a similar way to Java package names, specify subdirectories as well. 'require 'foo/bar' will look for a 'foo' subdirectory containing 'bar.rb'
matthew smillie.
···
On Aug 9, 2006, at 17:26, Jean Nibee wrote:
Hi
I'm coming from a java world and I'm trying to understand the structure
of a Ruby project and how 'require's are resolved.
In java we have this nice package routine where we put our .java files
into a directory hierarchy that is nice and neat.
Can I do the same in Ruby (I assume yes) but if I do how are "requires"
resolved when needed and what happend if I move code around the
hierarchy; are the resolve paths relative or abosolute based on some
'root' directory I"m unaware of.
(Currently to learn and test all my .rb files are in one directory...
it's getting messy)
I'm coming from a java world and I'm trying to understand the structure
of a Ruby project and how 'require's are resolved.
In java we have this nice package routine where we put our .java files
into a directory hierarchy that is nice and neat.
Can I do the same in Ruby (I assume yes) but if I do how are "requires"
resolved when needed and what happend if I move code around the
hierarchy; are the resolve paths relative or abosolute based on some
'root' directory I"m unaware of.
(Currently to learn and test all my .rb files are in one directory...
it's getting messy)
I'll see if I can explain a little. A require searches Ruby's load path with the required path being relative from each location listed in there. You can find Ruby's load path on your box with something like: