[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Splitting source code

Luke Worth

7/12/2005 3:10:00 AM

Hi. Coming from a c++/java background, i'm having a bit of trouble in
deciding how to split up source code. I currently have this:

#----program_tree.rb
require 'gtk2'
require 'program_tree_populate'

module HSGui
# GtkTreeStore is the generic GtkTreeModel implementation
class ProgramTree < Gtk::TreeStore
def initialize(source_code)
super(String)
populate(source_code)
end
end
end

#----program_tree_populate.rb
require 'gtk2'
module HSGui
class ProgramTree < Gtk::TreeStore
def populate(source_code)
#etc...
end
end
end


This seems slightly dodgy to me... Would anyone do this some other way?
(like define in a module then include the module)
--
Luke Worth




7 Answers

Austin Ziegler

7/12/2005 4:30:00 AM

0

On 7/11/05, Luke Worth <luke@worth.id.au> wrote:
> Hi. Coming from a c++/java background, i'm having a bit of trouble in
> deciding how to split up source code. I currently have this:
...
> This seems slightly dodgy to me... Would anyone do this some other way?
> (like define in a module then include the module)

I personally think that's very dodgy. In general, most people define
Ruby in such a way as to logically place that which belongs together
together. That may mean that there is more than one class defined in a
single file. I find it very rare to find that a single class is
defined over multiple files, except in the possible case of a C
extension.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Gavin Kistner

7/12/2005 5:03:00 AM

0

On Jul 11, 2005, at 10:29 PM, Austin Ziegler wrote:
> I find it very rare to find that a single class is
> defined over multiple files, except in the possible case of a C
> extension.

FWIW, I did this something like this with my ValidForm library. A
single file defined a base set of functionality for many classes, and
then a second file could optionally be included that added a custom
#to_html method to every one of those classes.

If I had to do it again, I might have subclassed all the originals in
a separate file...but then again I might not have.

It feels sketchy-but-acceptable to me to have FileB require FileA to
be meaningful, as long as FileA can stand on its own. It feels
pointless to me to have FileA and FileB mutually require each other
to be useful, for if that's the case, why not have them as one file?

Daniel Brockman

7/12/2005 4:15:00 PM

0

Gavin Kistner

7/13/2005 1:29:00 AM

0

On Jul 12, 2005, at 10:15 AM, Daniel Brockman wrote:
>> It feels sketchy-but-acceptable to me to have FileB require FileA to
>> be meaningful, as long as FileA can stand on its own.
>>
>
> Isn't this a typical and completely normal dependency?

Damn, I was afraid someone might notice that :)

Yes, it is. Yet I can't figure out what the difference is between my
Bar class requiring Foo from another file to make sense, versus my
Bar class requiring other pieces of Bar defined in another file.
Something smells slightly off (and yet it's what I was describing
that I did previously). *shrug*


Austin Ziegler

7/13/2005 1:29:00 AM

0

On 7/12/05, Daniel Brockman <daniel@brockman.se> wrote:
> Gavin Kistner <gavin@refinery.com> writes:
>> It feels sketchy-but-acceptable to me to have FileB require FileA to
>> be meaningful, as long as FileA can stand on its own.
> Isn't this a typical and completely normal dependency?

IMO, it's not what was originally described by the OP, which was something like:

A.rb:
require 'B'
class Foo; def initialize; process(); end; end

B.rb:
class Foo; def process(); end; end

IMO, classes should -- as much as is possible -- have all of their
definition in a single location, using alternate locations only as
necessary.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Luke Worth

7/13/2005 3:58:00 AM

0

> IMO, classes should -- as much as is possible -- have all of their
> definition in a single location, using alternate locations only as
> necessary.
Righto then, it's just that the function was originally 80 lines long
and took up room in my editor. its now 30 (original way was stupid).
If this were c++, i could have had

file a: class x { void process(); }

file b: void x::process() {}

and was wondering if a similar thing is possible with ruby - apparently
not.
Perhaps i need to use an editor that can fold methods instead of
emacs :P
--
Luke Worth




Austin Ziegler

7/13/2005 11:21:00 AM

0

On 7/12/05, Luke Worth <luke@worth.id.au> wrote:
>> IMO, classes should -- as much as is possible -- have all of
>> their definition in a single location, using alternate locations
>> only as necessary.
> Righto then, it's just that the function was originally 80 lines
> long and took up room in my editor. its now 30 (original way was
> stupid). If this were c++, i could have had
>
> file a: class x { void process(); }
>
> file b: void x::process() {}
>
> and was wondering if a similar thing is possible with ruby -
> apparently not. Perhaps i need to use an editor that can fold
> methods instead of emacs :P

Sure, it's possible. It's just not necessasrily advisable. In the
case of C++, file A would have been "x.h", and Ruby doesn't really
have the concept of an include file (only a required resource, which
currently maps to a file).

Most of my project files are several *hundred* lines long, but I try
to keep each method as short as possible, and only tend to include
that which is necessary in a given file.

I also tend to treat Ruby a little like Java, defining one "logical"
class per file (like Ruwiki and PDF::Writer), only defining multiple
classes in a file when they are subclasses of Exception related to
the main class of the file. On the other hand, in
Archive::Tar::Minitar, almost everything is defined in a single
class.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca