Splitting source code

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

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

···

On 7/11/05, Luke Worth <luke@worth.id.au> wrote:
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

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?

···

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.

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?

···

--
Daniel Brockman <daniel@brockman.se>

Damn, I was afraid someone might notice that :slight_smile:

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*

···

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?

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

···

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?

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

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 :stuck_out_tongue:

···

--
Luke Worth

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

···

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 :stuck_out_tongue:

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