How to split ruby code into multiple files

Discussion in 'Ruby' started by Cedric Vicenti, Oct 20, 2007.

  1. 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-forum.com/.
    Cedric Vicenti, Oct 20, 2007
    #1
    1. Advertising

  2. Cedric Vicenti

    Tim Hunter Guest

    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/projects/rmagick/]
    RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618]
    RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]
    Tim Hunter, Oct 20, 2007
    #2
    1. Advertising

  3. Cedric Vicenti

    Thomas Adam Guest

    On 20/10/2007, 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).


    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
    Thomas Adam, Oct 20, 2007
    #3
  4. Cedric Vicenti

    7stud -- Guest

    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-forum.com/.
    7stud --, Oct 20, 2007
    #4
  5. Cedric Vicenti

    Thufir Guest

    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?


    -Thufir
    Thufir, Oct 21, 2007
    #5
  6. Cedric Vicenti

    Thomas Adam Guest

    On 21/10/2007, Thufir <> 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
    Thomas Adam, Oct 21, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willempie
    Replies:
    2
    Views:
    482
    Kevin Spencer
    Mar 1, 2005
  2. Replies:
    4
    Views:
    938
    M.E.Farmer
    Feb 13, 2005
  3. Ilyas
    Replies:
    1
    Views:
    742
    Kevin Spencer
    Feb 19, 2008
  4. Replies:
    11
    Views:
    194
  5. satyam
    Replies:
    9
    Views:
    263
    Peter Otten
    Oct 24, 2012
Loading...

Share This Page