Namespacing my classes

Discussion in 'Ruby' started by Zouplaz, Oct 13, 2008.

  1. Zouplaz

    Zouplaz Guest

    Hello, as I want to avoid name clashes I tried to organise them like
    this (just a fake example, reflecting reality)

    lib/mylib.rb contains
    module Mylib

    module TestDatas
    FOO = "Ho yeah"
    end

    require 'somelib/tools'
    require 'somelib/motor'
    end

    somelib/tools.rb contains
    class Mylib::Tools
    end

    somelib/motor.rb contains
    class Mylib::Motor

    def something()
    Mylib::TestDatas.FOO
    end

    end



    Well, I did this by try/error until I had no more execution errors and
    having them behaving as expected but is there any caveat ?

    I'm just suprised by the location of the requires statements

    Thanks
     
    Zouplaz, Oct 13, 2008
    #1
    1. Advertising

  2. Zouplaz <> writes:

    > Hello, as I want to avoid name clashes I tried to organise them like
    > this (just a fake example, reflecting reality)
    >
    > lib/mylib.rb contains
    > module Mylib
    >
    > module TestDatas
    > FOO = "Ho yeah"
    > end
    >
    > require 'somelib/tools'
    > require 'somelib/motor'
    > end
    >
    > somelib/tools.rb contains
    > class Mylib::Tools
    > end
    >
    > somelib/motor.rb contains
    > class Mylib::Motor
    >
    > def something()
    > Mylib::TestDatas.FOO
    > end
    >
    > end
    >
    >
    >
    > Well, I did this by try/error until I had no more execution errors and
    > having them behaving as expected but is there any caveat ?
    >
    > I'm just suprised by the location of the requires statements


    You don't need to put these requires inside the module blocks.

    Instead, you could write:

    (module MyLib
    (module TestData
    (FOO = "abc")
    end)
    end)

    (module MyLib
    (class Tool
    end)
    end)

    (module MyLib
    (class Motor
    (def something
    (TestData::FOO)
    end)
    end)
    end)

    That is, if you avoid the (class Abc::Def::Ghi ... end) form, then
    modules namespaces will be created automatically when needed.

    Then, how you spread the forms over files and execute require is up to
    you and doesn't matter much.

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Oct 13, 2008
    #2
    1. Advertising

  3. On Oct 13, 9:12=A0am, (Pascal J. Bourguignon)
    wrote:
    > (module MyLib
    > =A0 (module TestData
    > =A0 =A0 (FOO =3D "abc")
    > =A0 end)
    > end)
    >
    > (module MyLib
    > =A0 (class Tool
    > =A0 end)
    > end)
    >
    > (module MyLib
    > =A0 (class Motor
    > =A0 =A0 (def something
    > =A0 =A0 =A0 (TestData::FOO)
    > =A0 =A0 end)
    > =A0 end)
    > end)


    What are all those parentheses about?

    --
    -yossef
     
    Yossef Mendelssohn, Oct 13, 2008
    #3
  4. On 13.10.2008 18:49, Yossef Mendelssohn wrote:
    > On Oct 13, 9:12 am, (Pascal J. Bourguignon)
    > wrote:
    >> (module MyLib
    >> (module TestData
    >> (FOO = "abc")
    >> end)
    >> end)
    >>
    >> (module MyLib
    >> (class Tool
    >> end)
    >> end)
    >>
    >> (module MyLib
    >> (class Motor
    >> (def something
    >> (TestData::FOO)
    >> end)
    >> end)
    >> end)

    >
    > What are all those parentheses about?


    Probably a Lisp fan. :)

    $ ruby -cw <<XXX
    > (module MyLib
    > (module TestData
    > (FOO = "abc")
    > end)
    > end)
    > XXX

    Syntax OK


    To OP, usually this is structured a bit differently:

    lib/mylib.rb contains

    module MyLib

    module TestData
    FOO = "Ho yeah"
    end

    end

    require 'mylib/tools'
    require 'mylib/motor'


    mylib/tools.rb contains

    module MyLib
    class Tools
    end
    end

    mylib/motor.rb contains

    module MyLib

    class Motor
    def something()
    TestData::FOO
    end
    end

    end


    Btw, there is no plural of "data" in English AFAIK.

    Kind regards

    robert
     
    Robert Klemme, Oct 13, 2008
    #4
  5. Zouplaz

    Zouplaz Guest

    le 13/10/2008 19:44, Robert Klemme nous a dit:

    >
    > require 'mylib/tools'
    > require 'mylib/motor'
    >
    >
    > mylib/tools.rb contains
    >
    > module MyLib
    > class Tools
    > end
    > end
    >
    > mylib/motor.rb contains
    >
    > module MyLib
    >
    > class Motor
    > def something()
    > TestData::FOO
    > end
    > end
    >
    > end
    >
    >
    > Btw, there is no plural of "data" in English AFAIK.
    >


    Ho, fine ! I didn't know I could reuse Module MyLib in several place to
    enclose the classes...

    About 'data', I trust you ;-) I wish I could find a webchat with people
    willing to speak French with me, while speaking English with them - I
    could learn a alot, but for the moment, I just found men masturbating in
    the dark :))
     
    Zouplaz, Oct 13, 2008
    #5
  6. On 13.10.2008 21:12, Zouplaz wrote:
    > le 13/10/2008 19:44, Robert Klemme nous a dit:
    >
    >>
    >> require 'mylib/tools'
    >> require 'mylib/motor'
    >>
    >>
    >> mylib/tools.rb contains
    >>
    >> module MyLib
    >> class Tools
    >> end
    >> end
    >>
    >> mylib/motor.rb contains
    >>
    >> module MyLib
    >>
    >> class Motor
    >> def something()
    >> TestData::FOO
    >> end
    >> end
    >>
    >> end
    >>
    >>
    >> Btw, there is no plural of "data" in English AFAIK.
    >>

    >
    > Ho, fine ! I didn't know I could reuse Module MyLib in several place to
    > enclose the classes...


    Note also that you do not need the full namespace path when referencing
    the const (see above).

    > About 'data', I trust you ;-) I wish I could find a webchat with people
    > willing to speak French with me, while speaking English with them - I
    > could learn a alot, but for the moment, I just found men masturbating in
    > the dark :))


    Well, you never know what it is good for. :)

    There is an IRB chat on freenode - albeit in English.

    Cheers

    robert
     
    Robert Klemme, Oct 13, 2008
    #6
  7. Robert Klemme wrote:
    >
    > There is an IRB chat on freenode - albeit in English.
    >


    IRB chat! Now that's an awesome typo. Or maybe a Freudian slip? You
    know you're a programmer when the distinction between IRB and IRC is
    blurred.
    --
    Posted via http://www.ruby-forum.com/.
     
    The Higgs bozo, Oct 13, 2008
    #7
  8. On 13.10.2008 22:43, The Higgs bozo wrote:
    > Robert Klemme wrote:
    >> There is an IRB chat on freenode - albeit in English.

    >
    > IRB chat! Now that's an awesome typo. Or maybe a Freudian slip? You
    > know you're a programmer when the distinction between IRB and IRC is
    > blurred.


    LOL! It was definitively unconscious. I was probably distracted. I
    leave the interpretation to you. :)

    Cheers

    robert


    PS: Are you the unknown particle that they now cannot find because the
    collider at Cern is broken?
     
    Robert Klemme, Oct 14, 2008
    #8
    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. KC

    Proper namespacing

    KC, Feb 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    364
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Feb 8, 2005
  2. =?Utf-8?B?ZGFGb3U=?=

    How to register an HTTPhandler with custom namespacing

    =?Utf-8?B?ZGFGb3U=?=, Nov 16, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    459
    =?Utf-8?B?ZGFGb3U=?=
    Nov 16, 2005
  3. Dan Webb

    Packaging / Namespacing JavaScript

    Dan Webb, Jul 13, 2004, in forum: Javascript
    Replies:
    6
    Views:
    144
    Richard Cornford
    Jul 15, 2004
  4. Khamis Abuelkomboz

    question about namespacing in perl

    Khamis Abuelkomboz, May 2, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    100
    Tassilo v. Parseval
    May 2, 2005
  5. Peter Michaux

    pseudo-namespacing in JavaScript

    Peter Michaux, Feb 22, 2008, in forum: Javascript
    Replies:
    14
    Views:
    369
    dhtml
    Feb 25, 2008
Loading...

Share This Page