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
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-
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
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 L.
[email protected]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: [email protected]
url: http://www.u-aizu.ac.jp
bib: Web of Science
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 [email protected]
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: [email protected]
url: http://www.u-aizu.ac.jp
bib: Web of Science
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
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”.
Thomas L. 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.