Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby namespace question

Reply
Thread Tools

Ruby namespace question

 
 
Zhao Yi
Guest
Posts: n/a
 
      01-21-2009
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/.

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      01-21-2009

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.



 
Reply With Quote
 
 
 
 
Zhao Yi
Guest
Posts: n/a
 
      01-21-2009
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/.

 
Reply With Quote
 
Tom Cloyd
Guest
Posts: n/a
 
      01-21-2009
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
<< http://www.velocityreviews.com/forums/(E-Mail Removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~


 
Reply With Quote
 
Zhao Yi
Guest
Posts: n/a
 
      01-21-2009
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/.

 
Reply With Quote
 
Justin Collins
Guest
Posts: n/a
 
      01-21-2009
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

 
Reply With Quote
 
Zhao Yi
Guest
Posts: n/a
 
      01-21-2009
yes,

logger = Log4r::Logger.new

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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      01-21-2009
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com ASP .Net 0 01-06-2006 11:31 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM
Namespace: Is it a scope or a namespace? Anonymous C++ 3 08-18-2003 01:31 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments