From: Matthew Smillie Date: 2006-08-10T01:39:00+09:00 Subject: Re: Code structure and using "require" 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) 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 $: A concrete example: irb(main):001:0> $: => ["/Users/matt/PhD/scripts", "/usr/local/lib/ruby/site_ruby/1.8", "/ usr/local/lib/ruby/site_ruby/1.8/powerpc-darwin8.4.0", "/usr/local/ lib/ruby/site_ruby", "/usr/local/lib/ruby/1.8", "/usr/local/lib/ruby/ 1.8/powerpc-darwin8.4.0", "."] 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.