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
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
== 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?
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
···
2009/12/16 Frank Guerino <frank.guerino@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?
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.
···
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.
--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/
It appears that I have to clarify myself a little better (sorry about
that). I currently have one long main file that has "both" Classes and
Methods. The Methods are "not" in the Classes and are completely
stand-alone.
The above examples clearly show how to split Classes across multiple
files and I get how that works. Thank you for that.
In addition to breaking Classes out to separate files and including them
through Modules, can I also split out the stand-alone "Methods" into
other files.
The Methods are very long and the initial stages of the refactoring
effort include simply splitting the methods into separate files to
reduce the main file size.
Can I somehow define Methods in separate files, aggregate them into a
Module and then Require that Module in my main file?
It appears that I have to clarify myself a little better (sorry about
that). I currently have one long main file that has "both" Classes and
Methods. The Methods are "not" in the Classes and are completely
stand-alone.
No method is completely stand-alone in Ruby. Methods that appear to be
stand-alone are actually defined on class Object, which everything else
inherits from. And that's usually poor practice. You generally want to
put your methods into an appropriate class.
The above examples clearly show how to split Classes across multiple
files and I get how that works. Thank you for that.
In addition to breaking Classes out to separate files and including them
through Modules, can I also split out the stand-alone "Methods" into
other files.
Yes, but it would be more Rubyish if you found classes to put them into.
The Methods are very long and the initial stages of the refactoring
effort include simply splitting the methods into separate files to
reduce the main file size.
No. The initial refactoring should involve making the methods shorter.
Can I somehow define Methods in separate files, aggregate them into a
Module and then Require that Module in my main file?
Yes. But you'd be better served to do this in a more OO way.