From: Brian Candler Date: 2009-01-21T19:47:13+09:00 Subject: Re: Ruby namespace question 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/.