Forum: Ruby Ruby namespace question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-21 02:13
I require two ruby files which include two classes with the same name.
how can I specify which class I use?
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-01-21 02:45
(Received via mailing list)
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.
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-21 02:51
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?
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-21 03:16
(Received via mailing list)
Zhao Yi wrote:
>>
>> 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
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-21 03:21
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
6e366eb5a71be2bad7f383d42aeb4788?d=identicon&s=25 Justin Collins (Guest)
on 2009-01-21 03:30
(Received via mailing list)
Zhao Yi wrote:
>
>

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
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-21 03:33
yes,

logger = Log4r::Logger.new

works. thanks.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-21 11:48
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
This topic is locked and can not be replied to.