Ruby namespace question

Discussion in 'Ruby' started by Zhao Yi, Jan 21, 2009.

  1. Zhao Yi

    Zhao Yi Guest

    I require two ruby files which include two classes with the same name.
    how can I specify which class I use?
    --
    Posted via http://www.ruby-forum.com/.
     
    Zhao Yi, Jan 21, 2009
    #1
    1. Advertising

  2. Zhao Yi

    Ryan Davis Guest

    On Jan 20, 2009, at 17:11 , Zhao Yi wrote:

    > I require two ruby files which include two classes with the same name.
    > how can I specify which class I use?


    class X; end # x.rb
    class X; end # y.rb

    same class, only one to specify. Files mean nothing in ruby, they're
    just vehicles for the parser.
     
    Ryan Davis, Jan 21, 2009
    #2
    1. Advertising

  3. Zhao Yi

    Zhao Yi Guest

    Ryan Davis wrote:
    > On Jan 20, 2009, at 17:11 , Zhao Yi wrote:
    >
    >> I require two ruby files which include two classes with the same name.
    >> how can I specify which class I use?

    >
    > class X; end # x.rb
    > class X; end # y.rb
    >
    > same class, only one to specify. Files mean nothing in ruby, they're
    > just vehicles for the parser.


    For this code:

    require 'x.rb'
    require 'y.rb'
    X.new #which class it uses, can I specify the class like x.X or y.X?
    --
    Posted via http://www.ruby-forum.com/.
     
    Zhao Yi, Jan 21, 2009
    #3
  4. Zhao Yi

    Tom Cloyd Guest

    Zhao Yi wrote:
    > Ryan Davis wrote:
    >
    >> On Jan 20, 2009, at 17:11 , Zhao Yi wrote:
    >>
    >>
    >>> I require two ruby files which include two classes with the same name.
    >>> how can I specify which class I use?
    >>>

    >> class X; end # x.rb
    >> class X; end # y.rb
    >>
    >> same class, only one to specify. Files mean nothing in ruby, they're
    >> just vehicles for the parser.
    >>

    >
    > For this code:
    >
    > require 'x.rb'
    > require 'y.rb'
    > X.new #which class it uses, can I specify the class like x.X or y.X?
    >

    Why not answer this empirically? Try it out!

    t.

    --

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
    Bellingham, Washington, U.S.A: (360) 920-1226
    << >> (email)
    << TomCloyd.com >> (website)
    << sleightmind.wordpress.com >> (mental health weblog)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    Tom Cloyd, Jan 21, 2009
    #4
  5. Zhao Yi

    Zhao Yi Guest

    Tom Cloyd wrote:
    > Why not answer this empirically? Try it out!


    I have tried but failed. This is my code:

    logger=log4r.Logger.new

    and I got the error: undefined local variable or method log4r

    --
    Posted via http://www.ruby-forum.com/.
     
    Zhao Yi, Jan 21, 2009
    #5
  6. Zhao Yi wrote:
    > Tom Cloyd wrote:
    >
    >> Why not answer this empirically? Try it out!
    >>

    >
    > I have tried but failed. This is my code:
    >
    > logger=log4r.Logger.new
    >
    > and I got the error: undefined local variable or method log4r
    >
    >


    I think you want

    logger = Log4r.Logger.new

    or

    logger = Log4r::Logger.new

    But, glancing at the docs, you also need to specify a name for it, like

    logger = Log4r::Logger.new "mylog"


    Hope that helps.

    -Justin
     
    Justin Collins, Jan 21, 2009
    #6
  7. Zhao Yi

    Zhao Yi Guest

    Zhao Yi, Jan 21, 2009
    #7
  8. Zhao Yi wrote:
    > Ryan Davis wrote:
    >> On Jan 20, 2009, at 17:11 , Zhao Yi wrote:
    >>
    >>> I require two ruby files which include two classes with the same name.
    >>> how can I specify which class I use?

    >>
    >> class X; end # x.rb
    >> class X; end # y.rb
    >>
    >> same class, only one to specify. Files mean nothing in ruby, they're
    >> just vehicles for the parser.

    >
    > For this code:
    >
    > require 'x.rb'
    > require 'y.rb'
    > X.new #which class it uses, can I specify the class like x.X or y.X?


    Nope - there is only one class X. Which file it was (first) defined in
    makes no difference. If you require 'x.rb' first then class X is
    created, and when you require 'y.rb' new methods are added into the
    *same* class.

    If you want them to be different, define them in different namespaces.
    This is what Log4r:: does (it refers to the namespace, not the file)

    module One; class X; end; end # x.rb
    module Two; class X; end; end # y.rb

    require 'x'
    require 'y'
    One::X.new # this is the one defined in x.rb
    Two::X.new # this is the one defined in y.rb
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jan 21, 2009
    #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. Èý¹â
    Replies:
    1
    Views:
    573
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,136
  3. Anonymous
    Replies:
    3
    Views:
    534
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    452
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,368
    Howard
    Nov 29, 2004
Loading...

Share This Page