I like to order my program code files as follows:

  Initial comment header

  require statements, etc.

  "main" code

  method definitions

Unfortunately, Ruby has a "single pass" execution flow
(a bit reminiscent of Pascal's single-pass compiler :-/)
that doesn't allow this order to be used, by default.


My current hack is to wrap the "main" code in a method
(e.g., main :-) and invoke it at the end of the file, I
can get around this issue.  I also like the fact that it
(a) puts the code at the same level of indentation as the
code in my methods and (b) limits the scope of variables:

  #!/usr/bin/env ruby -w
  #
  # foo - code formatting demo

  def main
    bar
  end

  def bar
    puts 'Hi!'
  end

  main


However, I have two concerns with this approach:

  *  I might be missing a common Ruby idiom (at least,
     common within the small group of folks who like to
     order their code as I do).

  *  I might be in danger of running into some sort of
     "dynamic language issue", in which some method has
     to be defined early, because it gets used in main's
     _definition_.

     I don't know, unfortunately, whether this is a real
     concern.  Or, if it is, whether I could work around
     it by putting the location-sensitive definitions in
     front of main's definition.

Comments?  Clues?  Suggestions?

-r
-- 
http://www.cfcl.com/rdm            Rich Morin
http://www.cfcl.com/rdm/resume     rdm / cfcl.com
http://www.cfcl.com/rdm/weblog     +1 650-873-7841

Technical editing and writing, programming, and web development