How does Ruby handle namespace?

Discussion in 'Ruby' started by Zhao Yi, Nov 17, 2008.

  1. Zhao Yi

    Zhao Yi Guest

    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?
    --
    Posted via http://www.ruby-forum.com/.
    Zhao Yi, Nov 17, 2008
    #1
    1. Advertising

  2. 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:

    > In Java, it uses import to include other classes from other =20
    > 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 =20
    > "../../"
    > to indicate these dependency directories. Is there a standard or =20
    > better
    > way to declares package? Does ruby have something like CLASSPATH in
    > Java?
    > --=20
    > Posted via http://www.ruby-forum.com/.
    >



    Einar Magn=FAs Boson
    +354-661 1649

    Einar Magnús Boson, Nov 17, 2008
    #2
    1. Advertising

  3. 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 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 (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.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Nov 17, 2008
    #3
  4. Zhao Yi

    Tony Lawetta Guest

    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
    Tony Lawetta, Nov 23, 2008
    #4
  5. Zhao Yi

    Zhao Yi Guest

    Zhao Yi, Nov 24, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    641
    Joe Smith
    Jul 22, 2004
  2. Replies:
    0
    Views:
    5,129
  3. Owen Wong
    Replies:
    1
    Views:
    388
    Karl Seguin [MVP]
    Mar 17, 2006
  4. Leon
    Replies:
    2
    Views:
    524
  5. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    446
    hanumizzle
    Oct 6, 2006
Loading...

Share This Page