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
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
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
Martin B. wrote:
You can do:
== myModule.rb
module MyModule
eval File.open(‘test1.rb’).read
eval File.open(‘test2.rb’).read
endBut 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
Christian K. 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 Test1Then 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
=> nilmodule MyModule; Test = ::Test; end
=> TestMyModule::Test
=> TestObject.instance_eval { remove_const(:Test) }
=> TestTest
NameError: uninitialized constant Test
from (irb):8
from :0MyModule::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
Frank G. 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.
2009/12/16 Frank G. [email protected]:
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
On Wed, 16 Dec 2009 23:33:29 +0900, Brian C. wrote:
def method1
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.
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
Frank G. 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
[email protected]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs