newbie question - how to use multiple source files

Discussion in 'Ruby' started by Ben Hoffmann, Dec 30, 2007.

  1. Ben Hoffmann

    Ben Hoffmann Guest

    I'm coming from Java, and am trying to understand how to break upo code
    into more than one file. So in this example, I have one class in the
    first file, another in the second file, and I want to create an object
    from the first class, but from the second file. Environment - Aptana on
    XP machine. See below:

    1) First file named first_class_file.rb:

    class FirstClassFile

    public

    def initialize
    super
    puts "Hello from First Class"
    end
    end

    my1stObject = FirstClassFile.new


    2) Second file named second_class_file.rb:

    class SecondClassFile

    def initialize
    super
    puts "Hello from Second Class"
    end
    end

    my2ndObject = SecondClassFile.new

    my3rdObject = FirstClassFile.new

    3) Console result of running second file:

    Hello from Second Class
    second_class_file.rb:11: uninitialized constant FirstClassFile (NameError)

    Question - What obvious thing am I doing wrong?
    Thanks!
    - Ben
    Ben Hoffmann, Dec 30, 2007
    #1
    1. Advertising

  2. Subject: newbie question - how to use multiple source files
    Date: lun 31 dic 07 02:36:28 +0900

    Quoting Ben Hoffmann ():

    > I'm coming from Java, and am trying to understand how to break upo code
    > into more than one file. So in this example, I have one class in the first
    > file, another in the second file, and I want to create an object from the
    > first class, but from the second file. Environment - Aptana on XP machine.


    In the second file, before referring to the FirstClassFile class, you must add

    require 'first_class_file'

    and first_class_file.rb has to be either in your current directory, or
    in a directory included in your search path.

    Carlo

    --
    * Se la Strada e la sua Virtu' non fossero state messe da parte,
    * K * Carlo E. Prelz - che bisogno ci sarebbe
    * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)
    Carlo E. Prelz, Dec 30, 2007
    #2
    1. Advertising

  3. Ben Hoffmann

    Tim Hunter Guest

    Ben Hoffmann wrote:
    >
    > I'm coming from Java, and am trying to understand how to break upo code
    > into more than one file. So in this example, I have one class in the
    > first file, another in the second file, and I want to create an object
    > from the first class, but from the second file. Environment - Aptana on
    > XP machine. See below:


    Check out the "require" and "load" Kernel methods. The "require" method
    is what you want, but "load" also has its uses.

    --
    RMagick: http://rmagick.rubyforge.org/
    RMagick 2: http://rmagick.rubyforge.org/rmagick2.html
    Tim Hunter, Dec 30, 2007
    #3
  4. Ben Hoffmann

    HiddenBek Guest

    The second file must require the first:

    require 'first_class_file'

    This would generally go at the top of the file, but anywhere before
    FirstClassFile is referenced is fine. The require method looks at
    each path in the $LOAD_PATH global variable, then attempts to find a
    Ruby source file or C extension that matches the name you pass in.

    Check out http://www.rubycentral.com/pickaxe/tut_modules.html#S4 for
    more details.

    In your example, the public and super calls are unnecessary. Public
    is the default visibility, and, as both of your classes inherit from
    Object, there's no need to call the initializer of the superclass.
    It's also quite common to have multiple Ruby classes in the same
    source file. Ruby classes are often small, and many people find it
    easier to manage a small handful of larger files than dozens of tiny
    ones.

    On Dec 30, 1:36 pm, Ben Hoffmann <> wrote:
    > I'm coming from Java, and am trying to understand how to break upo code
    > into more than one file. So in this example, I have one class in the
    > first file, another in the second file, and I want to create an object
    > from the first class, but from the second file. Environment - Aptana on
    > XP machine. See below:
    >
    > 1) First file named first_class_file.rb:
    >
    > class FirstClassFile
    >
    > public
    >
    > def initialize
    > super
    > puts "Hello from First Class"
    > end
    > end
    >
    > my1stObject = FirstClassFile.new
    >
    > 2) Second file named second_class_file.rb:
    >
    > class SecondClassFile
    >
    > def initialize
    > super
    > puts "Hello from Second Class"
    > end
    > end
    >
    > my2ndObject = SecondClassFile.new
    >
    > my3rdObject = FirstClassFile.new
    >
    > 3) Console result of running second file:
    >
    > Hello from Second Class
    > second_class_file.rb:11: uninitialized constant FirstClassFile (NameError)
    >
    > Question - What obvious thing am I doing wrong?
    > Thanks!
    > - Ben
    HiddenBek, Dec 30, 2007
    #4
  5. Ben Hoffmann

    Dejan Dimic Guest

    On Dec 30, 6:36 pm, Ben Hoffmann <> wrote:
    > I'm coming from Java, and am trying to understand how to break upo code
    > into more than one file. So in this example, I have one class in the
    > first file, another in the second file, and I want to create an object
    > from the first class, but from the second file. Environment - Aptana on
    > XP machine. See below:
    >
    > 1) First file named first_class_file.rb:
    >
    > class FirstClassFile
    >
    > public
    >
    > def initialize
    > super
    > puts "Hello from First Class"
    > end
    > end
    >
    > my1stObject = FirstClassFile.new
    >
    > 2) Second file named second_class_file.rb:
    >
    > class SecondClassFile
    >
    > def initialize
    > super
    > puts "Hello from Second Class"
    > end
    > end
    >
    > my2ndObject = SecondClassFile.new
    >
    > my3rdObject = FirstClassFile.new
    >
    > 3) Console result of running second file:
    >
    > Hello from Second Class
    > second_class_file.rb:11: uninitialized constant FirstClassFile (NameError)
    >
    > Question - What obvious thing am I doing wrong?
    > Thanks!
    > - Ben


    Coming from Java I presume that you are familiar with import
    directive.
    Ruby has "require" for the same job as stated before.
    Dejan Dimic, Dec 30, 2007
    #5
  6. Ben Hoffmann

    Thufir Guest

    On Mon, 31 Dec 2007 02:36:28 +0900, Ben Hoffmann wrote:

    > So in this example, I have one class in the first file, another in the
    > second file, and I want to create an object from the first class,



    Hi :)

    I'm coming from java, too. Heads up: one of the big but silly
    differences is to not capitalize file names, like Foo.rb, instead call it
    foo.rb.

    How about something like the following (note the output of "ruby
    driver.rb"):



    thufir@arrakis ~/Desktop/foobar $ ll
    total 12
    -rw-r--r-- 1 thufir users 53 Dec 30 14:17 bar.rb
    -rw-r--r-- 1 thufir users 86 Dec 30 14:18 driver.rb
    -rw-r--r-- 1 thufir users 53 Dec 30 14:17 foo.rb
    thufir@arrakis ~/Desktop/foobar $
    thufir@arrakis ~/Desktop/foobar $ ruby driver.rb
    hello from a foo
    hello from a bar
    thufir@arrakis ~/Desktop/foobar $ cat driver.rb
    require 'foo.rb'
    require 'bar.rb'



    foo = Foo.new
    foo.hello

    bar = Bar.new
    bar.hello
    thufir@arrakis ~/Desktop/foobar $
    thufir@arrakis ~/Desktop/foobar $ cat foo.rb
    class Foo
    def hello
    puts "hello from a foo"
    end
    end
    thufir@arrakis ~/Desktop/foobar $
    thufir@arrakis ~/Desktop/foobar $ cat bar.rb
    class Bar
    def hello
    puts "hello from a bar"
    end
    end
    thufir@arrakis ~/Desktop/foobar $



    The driver, driver.rb, does all the work.




    You may be interested in the code at:
    http://code.google.com/p/dwemthys/source






    HTH,

    Thufir
    Thufir, Dec 30, 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. Replies:
    4
    Views:
    937
    M.E.Farmer
    Feb 13, 2005
  2. William E. Storey II

    Newbie Question: Split Programs in to multiple files.

    William E. Storey II, May 22, 2005, in forum: C Programming
    Replies:
    2
    Views:
    380
    CBFalconer
    May 23, 2005
  3. Frank Reiff
    Replies:
    9
    Views:
    136
    Frank Reiff
    Mar 27, 2007
  4. Jenny
    Replies:
    3
    Views:
    247
    Dag Sunde
    Dec 17, 2004
  5. Replies:
    17
    Views:
    84
    Rustom Mody
    May 22, 2014
Loading...

Share This Page