Prev: meta-programming
Next: thanks
From: Frank Guerino on 16 Dec 2009 07:10 Martin Boese wrote: > You can do: > > == myModule.rb > module MyModule > eval File.open('test1.rb').read > eval File.open('test2.rb').read > end > > > But this would be ugly, the better way IMO is to define the classes > individually within modules: > > == test.rb: > module MyModule > class Test > (...) > end > end > > == test2.rb: > module MyModule > class Test2 > (...) > end > end > > == myModule.rb > require 'test' > require 'test2' > > > Martin ========================= Hi, I've just started writing Ruby and have a very long file of, both, methods and files. I understand the above file partitioning methodology for classes. Does it also work for methods? If so, how would it look? Thanks very much for the help, Frank -- Posted via http://www.ruby-forum.com/.
From: Brian Candler on 16 Dec 2009 09:31 Christian Kerth wrote: > Hey! > > Is it possible to achieve somthing like this: > > I want to have multiple classes, each in one file > > e.g. > > test.rb -> class Test > test1.rb -> class Test1 > > Then i want to combine those classes into a module > > myModule.rb -> module myModule That depends what you mean by "combining those classes into a module". Do you mean that you want these classes to appear under the MyModule namespace? You could simply do: require 'test' require 'test1' module MyModule Test = ::Test Test1 = ::Test1 end Now you can use MyModule::Test.new (or whatever) You still have the top-level constants Test and Test1. You can get rid of them using remove_const, although it looks like the class will still think its name is "Test" rather than "MyModule::Test" >> class Test; end => nil >> module MyModule; Test = ::Test; end => Test >> MyModule::Test => Test >> Object.instance_eval { remove_const(:Test) } => Test >> Test NameError: uninitialized constant Test from (irb):8 from :0 >> MyModule::Test => Test >> But at the end of the day, I'd question what it is you're actually trying to do. Normally it would make more sense just to add module MyModule ... end to test.rb and test1.rb -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 16 Dec 2009 09:32 2009/12/16 Frank Guerino <frank.guerino(a)traverseit.com>: > I've just started writing Ruby and have a very long file of, both, > methods and files. I understand the above file partitioning methodology > for classes. Does it also work for methods? If so, how would it look? The same way file1.rb module M class X def meth1 end end end file2.rb module M class X def meth2 end end end Although I'd say that this looks awkward. IMHO it does not make sense to split a single class method by method on different files. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Brian Candler on 16 Dec 2009 09:33 Frank Guerino wrote: > I've just started writing Ruby and have a very long file of, both, > methods and files. I understand the above file partitioning methodology > for classes. Does it also work for methods? If so, how would it look? Here's one way: -- foo1.rb -- class Foo def method1 end end -- foo2.rb -- class Foo def method2 end end -- foo.rb -- require 'foo1' require 'foo2' But you don't see this very often, because it's unusual to have a single class containing hundreds of methods. Normally the problem partitions better into smaller classes. -- Posted via http://www.ruby-forum.com/.
From: Ken Bloom on 16 Dec 2009 13:08
On Wed, 16 Dec 2009 23:33:29 +0900, Brian Candler wrote: > Frank Guerino wrote: >> I've just started writing Ruby and have a very long file of, both, >> methods and files. I understand the above file partitioning >> methodology for classes. Does it also work for methods? If so, how >> would it look? > > Here's one way: > > -- foo1.rb -- > class Foo > def method1 > end > end > > -- foo2.rb -- > class Foo > def method2 > end > end > > -- foo.rb -- > require 'foo1' > require 'foo2' > > But you don't see this very often, because it's unusual to have a single > class containing hundreds of methods. Normally the problem partitions > better into smaller classes. The only thing to be aware of when defining parts of a class in different files is that only one file can declare the class's ancestor, and that has to be the first one loaded. -- Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/ |