module My; module Little; module Namespace; class Foo end; end; end; end <--- module My::Little::Namespace; class Foo end; end; ---> are those snippets exactly the same? Ie i can do: foo = My::Little::Namespace::Foo.new? thanks in advance for any help! George Moschovitis, http://www.joy.gr [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <797f8269.0401210951.58438d43@posting.google.com> [ref] => [htmlstate] => on_nl2br [postusername] => George [ip] => gm@navel.gr [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 1 [islastshown] => [isfirstshown] => 1 [attachments] => [allattachments] => ) --> > > module My; module Little; module Namespace; > > class Foo > end; > > end; end; end > > <--- > > module My::Little::Namespace; > > class Foo > end; > > end; > > ---> > > > are those snippets exactly the same? Ie i can do: > > foo = My::Little::Namespace::Foo.new?[/ref] First of all, you can only do this when the modules already exist. (How should the interpreter know whether Little is a class or module?) Second of all, I think you need parens because of a precedence issue: foo = (My::Little::Namespace::Foo).new? Refer to a question I asked last week, something like "Nested classes and the Singleton pattern", and see Nobu's reply. Hal [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <400EBE6A.4080106@hypermetrics.com> [ref] => <797f8269.0401210951.58438d43@posting.google.com> [htmlstate] => on_nl2br [postusername] => Hal [ip] => hal9000@hyperme [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 2 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> > > > > module My; module Little; module Namespace; > > > > class Foo > > end; > > > > end; end; end > > > > <--- > > > > module My::Little::Namespace; > > > > class Foo > > end; > > > > end;[/ref][/ref] That doesn't work. However, you can do irb(main):003:0> module Outer; module Inner; end; end => nil irb(main):004:0> class Outer::Inner::Foo;end => nil irb(main):005:0> Outer::Inner::Foo.new => # [ref][ref] > > are those snippets exactly the same? Ie i can do: > > > > foo = My::Little::Namespace::Foo.new?[/ref][/ref] Yes, you can. But that's not the same as code snippet 1 and code snippet 2 are "exactly the same". :-) [ref] > First of all, you can only do this when the modules already > exist. (How should the interpreter know whether Little is > a class or module?) > > Second of all, I think you need parens because of a > precedence issue: > > foo = (My::Little::Namespace::Foo).new? > > Refer to a question I asked last week, something like "Nested > classes and the Singleton pattern", and see Nobu's reply.[/ref] Hmmm... irb(main):001:0> module Outer; module Inner; class Foo; end; end; end => nil irb(main):002:0> Outer::Inner::Foo.new => # Regards robert [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => <797f8269.0401210951.58438d43@posting.google.com> <400EBE6A.4080106@hypermetrics.com> [htmlstate] => on_nl2br [postusername] => Robert [ip] => bob.news@gmx.ne [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 4 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> Modules as namespace - Ruby

Modules as namespace - Ruby

Hello everyone, I would like to now if the following snippets of code are equivalent: ---> module My; module Little; module Namespace; class Foo end; end; end; end <--- module My::Little::Namespace; class Foo end; end; ---> are those snippets exactly the same? Ie i can do: foo = My::Little::Namespace::Foo.new? thanks in advance for any help! George Moschovitis, http://www.joy.gr...

  1. #1

    Default Modules as namespace

    Hello everyone,

    I would like to now if the following snippets of code are equivalent:

    --->

    module My; module Little; module Namespace;

    class Foo
    end;

    end; end; end

    <---

    module My::Little::Namespace;

    class Foo
    end;

    end;

    --->


    are those snippets exactly the same? Ie i can do:

    foo = My::Little::Namespace::Foo.new?


    thanks in advance for any help!


    George Moschovitis, http://www.joy.gr
    George Guest

  2. #2

    Default Re: Modules as namespace

    George Moschovitis wrote: 

    First of all, you can only do this when the modules already
    exist. (How should the interpreter know whether Little is
    a class or module?)

    Second of all, I think you need parens because of a
    precedence issue:

    foo = (My::Little::Namespace::Foo).new?

    Refer to a question I asked last week, something like "Nested
    classes and the Singleton pattern", and see Nobu's reply.



    Hal




    Hal Guest

  3. #3

    Default Re: Modules as namespace

    Hi,

    In message "Re: Modules as namespace"
    on 04/01/22, Hal Fulton <com> writes:

    |Second of all, I think you need parens because of a
    |precedence issue:
    |
    | foo = (My::Little::Namespace::Foo).new?

    No, parens are not required. Ruby pr is smart enough to read your
    intention (this case at least).

    matz.


    Yukihiro Guest

  4. #4

    Default Re: Modules as namespace


    "Hal Fulton" <com> schrieb im Newsbeitrag
    news:com... [/ref]

    That doesn't work. However, you can do

    irb(main):003:0> module Outer; module Inner; end; end
    => nil
    irb(main):004:0> class Outer::Inner::Foo;end
    => nil
    irb(main):005:0> Outer::Inner::Foo.new
    => #<Outer::Inner::Foo:0x10194548>
     [/ref]

    Yes, you can. But that's not the same as code snippet 1 and code snippet 2
    are "exactly the same". :-)
     

    Hmmm...

    irb(main):001:0> module Outer; module Inner; class Foo; end; end; end
    => nil
    irb(main):002:0> Outer::Inner::Foo.new
    => #<Outer::Inner::Foo:0x10199f60>

    Regards

    robert

    Robert Guest

  5. #5

    Default Re: Modules as namespace

    On Thu, 22 Jan 2004, Yukihiro Matsumoto wrote:
     

    really? what am i missing?

    ~ > cat foo.rb

    attempts =

    lambda do
    module A::B::C
    class Foo; end
    end
    A::B::C::Foo.new
    end,

    lambda do
    module A
    module B
    module C
    class Foo; end
    end
    end
    end
    A::B::C::Foo.new
    end


    attempts.each do |block|
    begin
    p block.call
    rescue Exception => e
    p e
    end
    end


    ~ > ruby foo.rb
    #<NameError: uninitialized constant A>
    #<A::B::C::Foo:0x400a38a4>


    -a
    --

    ATTN: please update your address books with address below!

    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | STP :: http://www.ngdc.noaa.gov/stp/
    | NGDC :: http://www.ngdc.noaa.gov/
    | NESDIS :: http://www.nesdis.noaa.gov/
    | NOAA :: http://www.noaa.gov/
    | US DOC :: http://www.commerce.gov/
    |
    | The difference between art and science is that science is what we
    | understand well enough to explain to a computer.
    | Art is everything else.
    | -- Donald Knuth, "Discover"
    |
    | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
    ================================================== =============================

    Ara.T.Howard Guest

  6. #6

    Default Re: Modules as namespace

    On Thu, 22 Jan 2004 04:54:59 +0900
    "Ara.T.Howard" <ngdc.noaa.gov> wrote:
     

    Probably the fact A doesn't exist here.

    <snip>

    If you call that block first, you haven't declared anything yet.
    Try evaluating them in the reverse order.

    --
    Ryan Pavlik <com>

    "He said cheetos are orange, so they're just as good
    for you as *real* oranges." - 8BT


    Ryan Guest

  7. #7

    Default Re: Modules as namespace

    Yukihiro Matsumoto wrote: 

    Sorry to mislead. This is different from the case I was looking at
    earlier.

    Hal




    Hal Guest

  8. #8

    Default Re: Modules as namespace

    > First of all, you can only do this when the modules already 


    Thanks for the info, then perhaps a more flexible way to declare
    namespaces should be added in Ruby 1.9? Matz what do you think?

    George Moschovitis,
    George Guest

  9. #9

    Default Re: Modules as namespace

    Hi,

    In message "Re: Modules as namespace"
    on 04/01/24, George Moschovitis <gr> writes:

    |Thanks for the info, then perhaps a more flexible way to declare
    |namespaces should be added in Ruby 1.9? Matz what do you think?

    What do you mean by "flexible way to declare namespaces"?

    matz.


    Yukihiro Guest

  10. #10

    Default Re: Modules as namespace

    On Sat, 24 Jan 2004, Yukihiro Matsumoto wrote:
     


    perhaps :

    s
    e p e
    n m a c
    a


    ??

    ;-)

    -a
    --

    ATTN: please update your address books with address below!

    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | STP :: http://www.ngdc.noaa.gov/stp/
    | NGDC :: http://www.ngdc.noaa.gov/
    | NESDIS :: http://www.nesdis.noaa.gov/
    | NOAA :: http://www.noaa.gov/
    | US DOC :: http://www.commerce.gov/
    |
    | The difference between art and science is that science is what we
    | understand well enough to explain to a computer.
    | Art is everything else.
    | -- Donald Knuth, "Discover"
    |
    | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
    ================================================== =============================

    Ara.T.Howard Guest

  11. #11

    Default Re: Modules as namespace

    > What do you mean by "flexible way to declare namespaces"?

    instead of

    module My; module Little; module Namespace
    class MyClass
    end
    end; end; end;

    something like:

    namespace My::Little::Namespace
    class MyClass
    end
    end

    excuse me for the improper use of 'flexible' :(

    George Moschovitis
    George Guest

  12. #12

    Default Re: Modules as namespace


    "George Moschovitis" <gr> schrieb im Newsbeitrag
    news:google.com... 
    >
    > instead of
    >
    > module My; module Little; module Namespace
    > class MyClass
    > end
    > end; end; end;
    >
    > something like:
    >
    > namespace My::Little::Namespace
    > class MyClass
    > end
    > end[/ref]

    Well, you can at least do this:

    $ irb --prompt simple [/ref]
    ?> nested.split('::').each do |sym|
    ?> raise NameError, "Symbol must start uppercase" unless /^[A-Z]/ =~
    sym [/ref]
    ?> eval( pre + post, bind ) [/ref]
    => nil [/ref]
    => Kernel::Foo::Bar [/ref]
    => Bar::Foo [/ref]
     

    :-))

    Regards

    robert

    Robert Guest

  13. #13

    Default Re: Modules as namespace

    Hi --

    On Sun, 25 Jan 2004, George Moschovitis wrote:
     
    >
    > instead of
    >
    > module My; module Little; module Namespace
    > class MyClass
    > end
    > end; end; end;
    >
    > something like:
    >
    > namespace My::Little::Namespace
    > class MyClass
    > end
    > end[/ref]

    You can do essentially that, as long as the rightmost one is the only
    undefined one (since otherwise it's impossible to tell whether they're
    supposed to be modules or classes):

    module A
    class B
    end
    end

    class A::B::C
    end

    etc.


    David

    --
    David A. Black
    net


    David Guest

  14. #14

    Default Re: Modules as namespace

    > $ irb --prompt simple [/ref][/ref]
    .... [/ref]
    >[/ref]

    wow, this is a cool idea! I forgot how ..flexible Ruby is ;-)

    Anyway something like this isnt usefull enough to be integrated by
    default in Ruby 2.0?

    George Moschovitis
    George Guest

  15. #15

    Default Re: Modules as namespace

    * Robert Klemme <net> [Jan, 24 2004 20:00]: [/ref]
    > ?> nested.split('::').each do |sym|
    > ?> raise NameError, "Symbol must start uppercase" unless /^[A-Z]/ =~
    > sym [/ref]
    > ?> eval( pre + post, bind ) [/ref]
    > => nil [/ref]
    > => Kernel::Foo::Bar [/ref]
    > => Bar::Foo [/ref][/ref]
    heh, sweet...one can really tell Ruby is inspired by Lisp :-)
    nikolai

    --
    ::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
    ::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
    ::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
    main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


    Nikolai Guest

  16. #16

    Default Re: Modules as namespace

    il Tue, 27 Jan 2004 07:30:12 +0900, Nikolai Weibull
    <org> ha scritto::

     [/ref]
    >heh, sweet...one can really tell Ruby is inspired by Lisp :-)
    > nikolai[/ref]

    nah, you can tell it fromn this
    http://www.ping.de/~flori/ruby/programs/functional.html
    ;)
    gabriele Guest

Similar Threads

  1. Suggested Modules... Namespace:Tk::TkDesigner?
    By jimicarlo in forum PERL Modules
    Replies: 3
    Last Post: February 21st, 10:35 PM
  2. namespace for PostgreSQL related modules?
    By Janwillem in forum PERL Modules
    Replies: 4
    Last Post: November 14th, 04:06 PM
  3. Namespace question for new SNMP modules
    By Eric Waters in forum PERL Modules
    Replies: 0
    Last Post: January 18th, 04:28 PM
  4. Namespace for apartment threading modules ?
    By Dean Arnold in forum PERL Modules
    Replies: 0
    Last Post: August 17th, 09:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •