[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby namespace question

Zhao Yi

1/21/2009 1:12:00 AM

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

7 Answers

Ryan Davis

1/21/2009 1:43:00 AM

0


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.



Zhao Yi

1/21/2009 1:50:00 AM

0

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

Tom Cloyd

1/21/2009 2:14:00 AM

0

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
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Zhao Yi

1/21/2009 2:20:00 AM

0

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

Justin Collins

1/21/2009 2:29:00 AM

0

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

Zhao Yi

1/21/2009 2:32:00 AM

0

yesï¼?

logger = Log4r::Logger.new

works. thanks.
--
Posted via http://www.ruby-....

Brian Candler

1/21/2009 10:47:00 AM

0

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