[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to split ruby code into multiple files

Cedric Vicenti

10/20/2007 6:34:00 PM

Hi guys,

my ruby code is now a long file and I'd like to split it into different
smaller files so that I could reuse them in different programs and make
my original code smaller at the same time.

One file should be used as the master and will reference the code of the
other files.

Is it feasible? Do I have to create some sort of a #include command or
create function objects (I just need a code split, I do not want to
create objects).

thank you.
--
Posted via http://www.ruby-....

5 Answers

Tim Hunter

10/20/2007 6:43:00 PM

0

Cedric Vicenti wrote:
> Hi guys,
>
> my ruby code is now a long file and I'd like to split it into different
> smaller files so that I could reuse them in different programs and make
> my original code smaller at the same time.
>
> One file should be used as the master and will reference the code of the
> other files.
>
> Is it feasible? Do I have to create some sort of a #include command or
> create function objects (I just need a code split, I do not want to
> create objects).
>
> thank you.

Use the require method to require individual files.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

Thomas Adam

10/20/2007 6:49:00 PM

0

On 20/10/2007, Cedric Vicenti <ced_dude@hotmail.com> wrote:
> Hi guys,
>
> my ruby code is now a long file and I'd like to split it into different
> smaller files so that I could reuse them in different programs and make
> my original code smaller at the same time.
>
> One file should be used as the master and will reference the code of the
> other files.
>
> Is it feasible? Do I have to create some sort of a #include command or
> create function objects (I just need a code split, I do not want to
> create objects).

You should create Modules and allow them to be mixins to your Classes
via the "include" word. Example:

Module M
def some_func
"I am some_func"
end
end

class A
include M
end

foo = A.new
puts a.report

If "Module M" were in its own file, you coud then just "require" that
file, for instance.

-- Thomas Adam

7stud --

10/20/2007 9:21:00 PM

0

Cedric Vicenti wrote:
> Hi guys,
>
> my ruby code is now a long file and I'd like to split it into different
> smaller files so that I could reuse them in different programs and make
> my original code smaller at the same time.
>

1) Here's a simple scheme:


my_lib.rb
--------
def greet
puts "hello world"
end

def show(num)
puts num
end



some_program.rb
---------------
require 'my_lib.rb'

greet #hello world
show(10) #10


But doing it that way can cause problems in this situation:


some_program.rb
---------------
require 'my_lib.rb'

def greet()
puts "hi"
end


greet #hi
show(10) #10

The greet method in some_program.rb hides the greet method in my_lib.rb.
So you can do this instead:

my_lib.rb
--------
module Lib
def Lib.greet
puts "hello world"
end

def Lib.show(num)
puts num
end
end



some_program.rb
---------------
require 'my_lib.rb'

def greet()
puts "Hi"
end

Lib.greet
Lib.show(10)

greet


--
Posted via http://www.ruby-....

Thufir Hawat

10/21/2007 8:15:00 AM

0

Thomas Adam

10/21/2007 9:48:00 AM

0

On 21/10/2007, Thufir <hawat.thufir@gmail.com> wrote:
> On Sun, 21 Oct 2007 03:49:25 +0900, Thomas Adam wrote:
>
> > You should create Modules and allow them to be mixins to your Classes
> > via the "include" word.
>
> Would this be sorta equivalent to creating a package in Java?

Ish. But unlike packages, you get a whole lot more with the use of
Modules. I certainly wouldn't try and liken them to packages though.

-- Thomas Adam