Split Module to multiple Files?

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

thx

···

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

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

···

On Wednesday 02 April 2008 12:24:01 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

thx

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

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

···

from :0
--
Posted via http://www.ruby-forum.com/.

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

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

···

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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

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/

Hi everyone,

Thanks for your help on this.

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?

Thanks again for all your help.

Frank

···

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

Frank Guerino wrote:

Hi everyone,

Thanks for your help on this.

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.

Thanks again for all your help.

Frank

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/.