Forum: Ruby Breaking large Ruby script into multiple files

Posted by Thomas Luedeke (tpl)
on 2012-04-19 00:37
On large Fortran projects, I'm accustomed to breaking them up into
separate module files.  The program then utilizes USE statements to
"glue" it together.

I'm starting to develop some larger Ruby scripts, and they get messy
very fast.  Once I define many functions at the top of the script, then
start the coding, I have an unwieldy mess.

How do you guys handle this in terms of organization and neatness?
Using multiple files and issuing system commands to run the external
Ruby files seem kinda dumb.  I also am not aware of commands to use
external function files (although I'm still a relative newbie).

Thanks in advance for any assistance!

TPL
Posted by jake kaiden (lljk)
on 2012-04-19 01:50
hi Thomas,

  you can use `require` to include any files that you want.

  try this - in the same directory, create two files: `first.rb` and 
`second.rb`

`first.rb` could look like this:

Class Traveller
  def initialize
    puts "I'm from the first.rb!"
  end
end

  ...and `second.rb` like this:

require './first.rb'
# note: require 'first' will usually also work

Traveller.new


  do a search for `ruby require` or `ruby require_relative`, and you'll 
find more info...

 hth-

  - j
Posted by Saji Hameed (Guest)
on 2012-04-19 01:52
(Received via mailing list)
I imagine that you are not using the object oriented features of ruby. 
It
is better to get
started with the OOP aspect.   Googleing may help you find articles such 
as
the below

http://techoctave.com/c7/posts/46-object-oriented-...

After you are getting comfortable, you may also read Olsen's book named
Design Patterns in Ruby..

All the best,
saji

On Thu, Apr 19, 2012 at 7:39 AM, Thomas Luedeke 
<lists@ruby-forum.com>wrote:

> Ruby files seem kinda dumb.  I also am not aware of commands to use
> external function files (although I'm still a relative newbie).
>
> Thanks in advance for any assistance!
>
> TPL
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


--

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
 email: saji@u-aizu.ac.jp
 url: http://www.u-aizu.ac.jp
 bib: http://www.researcherid.com/rid/B-9188-2009
Posted by Saji Hameed (Guest)
on 2012-04-19 02:13
(Received via mailing list)
Following on Jake's advice and noting that you are familiar with Fortran
modules, an easier first step would
be to use modules in Ruby and start from there.

The first.rb from Jake's example could be

module Statistics
  def correlation(a,b)
      # code for correlation
  end

  def stddev(a)
     # code for standard deviation
  end
end

In second.rb, you could do
  require "./first"
  include Statistics  # similar to USE in fortran

 a=[1,2,3,4]
 b=[0,0,0,0]

 correlation(a,b)
 stddev(a)

saji
--

On Thu, Apr 19, 2012 at 8:50 AM, jake kaiden <lists@ruby-forum.com> 
wrote:

>  def initialize
>
>
>


--

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
 email: saji@u-aizu.ac.jp
 url: http://www.u-aizu.ac.jp
 bib: http://www.researcherid.com/rid/B-9188-2009
Posted by Henry Maddocks (Guest)
on 2012-04-19 03:17
(Received via mailing list)
If you break your unwieldy script into smaller files you can use 
'require' or 'load' to load in your files and execute them. Most people 
put all their requires at the top of your main file but you can call 
them anywhere. The difference between require is that require will only 
load your external file once, ie. it won't re-load a file that has been 
previously required. load needs an actually file name and will load the 
file every time you call load.

When you break your script up I would suggest you put similar functions 
in the same file and name the file after those functions. By similar I 
mean the functions work on similar data or perform similar operations. 
Then you can wrap those functions in a class which may enable you to 
simplify the methods.

Once you've done this you'll be well on your way to being an OO ruby 
programmer.

Henry
Posted by Jan E. (jacques1)
on 2012-04-19 11:47
Hi,

Just a small note: When including a script, don't rely on the current 
directory (i. e. don't write something like require './myscript.rb'). 
The current directory depends on how the Ruby interpreter was invoked 
and may even change during runtime. Either use absolute paths or 
require_relative (in Ruby 1.9).

But I definitely agree with the previous posters that you should program 
object oriented rather than have a bunch of single functions (they're 
actually methods) floating around. While Ruby does allow classical 
procedural programming to some extend (or at least makes the code look 
like it), this is certainly not recommended for larger scripts. And Ruby 
isn't Fortran.

I think the best way to scructure your code is to put every class in its 
own file and then include them in certain "executable scripts".
Posted by Thomas Luedeke (tpl)
on 2012-10-02 20:54
Thomas Luedeke wrote in post #1057265:
> On large Fortran projects, I'm accustomed to breaking them up into
> separate module files.  The program then utilizes USE statements to
> "glue" it together.
>
> I'm starting to develop some larger Ruby scripts, and they get messy
> very fast.  Once I define many functions at the top of the script, then
> start the coding, I have an unwieldy mess.
>
> How do you guys handle this in terms of organization and neatness?
> Using multiple files and issuing system commands to run the external
> Ruby files seem kinda dumb.  I also am not aware of commands to use
> external function files (although I'm still a relative newbie).
>
> Thanks in advance for any assistance!
>
> TPL

Thanks, all.  And yes, my intention is to use the breaking up process in 
an O-O fashion, much like I do for Fortran-2003 coding.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.