Problem with ruby classes

Discussion in 'Ruby' started by Manisha Tripathy, Feb 2, 2009.

  1. Can Anyone please tell me how can we create an object of a class that is
    present in one ruby file, in another ruby file? I tried using .new()
    method but that doesn't seem to work.It simply cannot find the required
    file.
    It works when I create an object of class inside the same file ,but not
    in another file.

    For example , lets say there is a file , file1.rb which contains class
    File1
    there is another file named file2.rb , that contains another class File2

    I want to use the methods in file1.rb inside class File2 in file2.rb
    Can someone tell how can I do that?

    My codes are somewhat like this

    # file1.rb
    class File1
    def method1
    end
    end

    #file2.rb
    object = File1.new
    class File2
    end


    The error while running file2.rb is

    file2.rb:3: uninitialized constant File1 (NameError)
    --
    Posted via http://www.ruby-forum.com/.
    Manisha Tripathy, Feb 2, 2009
    #1
    1. Advertising

  2. Alle Monday 02 February 2009, Manisha Tripathy ha scritto:
    > Can Anyone please tell me how can we create an object of a class that is
    > present in one ruby file, in another ruby file? I tried using .new()
    > method but that doesn't seem to work.It simply cannot find the required
    > file.
    > It works when I create an object of class inside the same file ,but not
    > in another file.
    >
    > For example , lets say there is a file , file1.rb which contains class
    > File1
    > there is another file named file2.rb , that contains another class File2
    >
    > I want to use the methods in file1.rb inside class File2 in file2.rb
    > Can someone tell how can I do that?
    >
    > My codes are somewhat like this
    >
    > # file1.rb
    > class File1
    > def method1
    > end
    > end
    >
    > #file2.rb
    > object = File1.new
    > class File2
    > end
    >
    >
    > The error while running file2.rb is
    >
    > file2.rb:3: uninitialized constant File1 (NameError)


    Add

    require 'file1'

    somewhere before the call to File1.new in file2.rb

    Stefano
    Stefano Crocco, Feb 2, 2009
    #2
    1. Advertising

  3. Stefano Crocco wrote:
    > Alle Monday 02 February 2009, Manisha Tripathy ha scritto:
    >>
    >>
    >> #file2.rb
    >> object = File1.new
    >> class File2
    >> end
    >>
    >>
    >> The error while running file2.rb is
    >>
    >> file2.rb:3: uninitialized constant File1 (NameError)

    >
    > Add
    >
    > require 'file1'
    >
    > somewhere before the call to File1.new in file2.rb
    >
    > Stefano


    Thanks Stefano !!!
    I have already tried to use require but of no use.It says no file to
    load, where as both the files are in the same directory
    --
    Posted via http://www.ruby-forum.com/.
    Manisha Tripathy, Feb 2, 2009
    #3
  4. Make sure you don't include the extension

    Sent from my iPhone

    On 03/02/2009, at 3:33 AM, Manisha Tripathy <>
    wrote:

    > Stefano Crocco wrote:
    >> Alle Monday 02 February 2009, Manisha Tripathy ha scritto:
    >>>
    >>>
    >>> #file2.rb
    >>> object = File1.new
    >>> class File2
    >>> end
    >>>
    >>>
    >>> The error while running file2.rb is
    >>>
    >>> file2.rb:3: uninitialized constant File1 (NameError)

    >>
    >> Add
    >>
    >> require 'file1'
    >>
    >> somewhere before the call to File1.new in file2.rb
    >>
    >> Stefano

    >
    > Thanks Stefano !!!
    > I have already tried to use require but of no use.It says no file to
    > load, where as both the files are in the same directory
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Julian Leviston, Feb 3, 2009
    #4
    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. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,927
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,201
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,397
    Andrew Thompson
    May 31, 2004
  4. John Carter
    Replies:
    1
    Views:
    117
  5. snacktime
    Replies:
    1
    Views:
    108
    Nobuyoshi Nakada
    Sep 20, 2006
Loading...

Share This Page