# How does Ruby handle namespace?

**URL:** <https://www.ruby-forum.com/t/how-does-ruby-handle-namespace/153452>\
**Category:** Ruby\
**Created:** [November 17, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/how-does-ruby-handle-namespace/153452 "2008-11-17T12:57:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [November 17, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/how-does-ruby-handle-namespace/153452/1 "2008-11-17T12:57:18Z")

</div>

In Java, it uses import to include other classes from other packages. In  
ruby, I only found that require statement can do this. When the ruby  
scripts are located in different directories, I have to use the “…/…/”  
to indicate these dependency directories. Is there a standard or better  
way to declares package? Does ruby have something like CLASSPATH in  
Java?

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [November 17, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/how-does-ruby-handle-namespace/153452/2 "2008-11-17T13:42:00Z")

</div>

I’d say it’s probably quicker to read the Ruby documentation on these  
topics, rather than asking how Java is different, because Java _is_  
different.

A good start is [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/) - this is  
the free, 1st edition of the “Pickaxe” Programming Ruby book, written  
for ruby 1.6.

There is now a revised 2nd edition for 1.8, and the 3rd edition for 1.9  
is in beta. These are available from bookshops or [www.pragprog.com](http://www.pragprog.com) (in  
PDF, book, or combo pack)

The 1.6 stuff is pretty much still valid, it’s just that 1.8 adds new  
classes and methods.

> In Java, it uses import to include other classes from other packages. In  
> ruby, I only found that require statement can do this. When the ruby  
> scripts are located in different directories, I have to use the “…/…/”  
> to indicate these dependency directories. Is there a standard or better  
> way to declares package? Does ruby have something like CLASSPATH in  
> Java?

In brief:

“require ‘foo’” looks for file foo.rb, and loads and runs it (unless  
foo.rb has already been loaded, in which case it’s a no-op)

The search path is the array $LOAD\_PATH (or $: for short), and the  
record of which files have already been loaded is in $LOADED\_FEATURES

Namespaces are handled entirely separately, using class and module  
keywords. A Module defines a new namespace, and a Class is a Module  
which can be instantiated to create objects.

How you organise your files is up to you, but it is conventional to have  
a lib/ subdirectory for your project. Then

module Foo  
class Bar  
…  
end  
end

would typically be put into lib/foo/bar.rb

If you do this, then you can load it using

require ‘foo/bar’  
obj = Foo::Bar.new

All this requires is lib/ to be in your search path.

You can arrange this from the command line:

ruby -Ilib main.rb

or you can do something like this in your main program:

$LOAD\_PATH.unshift(File.dirname( **FILE** )+“/lib”)

If your main program is inside a bin/ directory then you’d do

$LOAD\_PATH.unshift(File.dirname( **FILE** )+“/…/lib”)

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [November 17, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/how-does-ruby-handle-namespace/153452/3 "2008-11-17T13:42:01Z")

</div>

commandline:

ruby -I includedir1:includedir2:etc app.rb  
(i.e “i”.upcase)

in ruby $: holds the include path

On 17.11.2008, at 11:53 , Zhao Yi wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Einar Magnús Boson  
+354-661 1649  
removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [November 23, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/how-does-ruby-handle-namespace/153452/4 "2008-11-23T14:20:43Z")

</div>

Zhao Yi wrote:

> When the ruby  
> scripts are located in different directories, I have to use the “…/…/”  
> to indicate these dependency directories. Is there a standard or better  
> way to declares package?

I usually add elements (path names) to the value of the Array “$:”  
(variable name is without the double quotes).

Tony

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [November 24, 2008, 1:28am UTC](https://www.ruby-forum.com/t/how-does-ruby-handle-namespace/153452/5 "2008-11-24T01:28:43Z")

</div>

OK, thanks very much for your information.
