How do I create an instance of a class in another rb file

Discussion in 'Ruby' started by brian.kejser@protexis.com, Sep 5, 2006.

  1. Guest

    Hi

    I have two rb files in the same folder.

    The first file is one.rb and defines an instanciable class named 'One'.
    The second file is two.rb and needs to create an instance of class 'One'
    as defined in one.rb.

    How do I import the class 'One' into two.rb?

    Thanks
    , Sep 5, 2006
    #1
    1. Advertising

  2. On Sep 4, 2006, at 7:21 PM, wrote:

    > Hi
    >
    > I have two rb files in the same folder.
    >
    > The first file is one.rb and defines an instanciable class named
    > 'One'.
    > The second file is two.rb and needs to create an instance of class
    > 'One'
    > as defined in one.rb.
    >
    > How do I import the class 'One' into two.rb?
    >


    require 'one'
    one = One.new


    > Thanks
    >
    >
    >
    >
    Logan Capaldo, Sep 5, 2006
    #2
    1. Advertising

  3. Guest

    Hi

    What happens if there is more than one class in 'one.rb'?



    -----Original Message-----
    From: "Michael Guterl" <>
    Sent: Mon, September 4, 2006 16:45
    To: "ruby-talk ML" <>
    Subject: Re: How do I create an instance of a class in another rb file

    On 9/4/06, <> wrote:
    >
    > Hi
    >
    > I have two rb files in the same folder.
    >
    > The first file is one.rb and defines an instanciable class named 'One'.
    > The second file is two.rb and needs to create an instance of class 'One'
    > as defined in one.rb.
    >
    > How do I import the class 'One' into two.rb?



    Assuming the only class defined in one.rb is 'One' then you should be able
    to do:
    require 'one'

    Thanks
    >
    >
    >
    >
    >

    Michael Guterl
    , Sep 5, 2006
    #3
  4. Guest

    Hi

    Thanks.

    I thought 'require' only imported files in the Ruby lib folder.




    -----Original Message-----
    From: "Logan Capaldo" <>
    Sent: Mon, September 4, 2006 16:33
    To: "ruby-talk ML" <>
    Subject: Re: How do I create an instance of a class in another rb file


    On Sep 4, 2006, at 7:21 PM, wrote:

    > Hi
    >
    > I have two rb files in the same folder.
    >
    > The first file is one.rb and defines an instanciable class named
    > 'One'.
    > The second file is two.rb and needs to create an instance of class
    > 'One'
    > as defined in one.rb.
    >
    > How do I import the class 'One' into two.rb?
    >


    require 'one'
    one = One.new


    > Thanks
    >
    >
    >
    >
    , Sep 5, 2006
    #4
  5. On Sep 4, 2006, at 8:15 PM, wrote:

    > Hi
    >
    > Thanks.
    >
    > I thought 'require' only imported files in the Ruby lib folder.
    >
    >


    Well it only loads files from paths that are in $: (aka $LOAD_PATH).
    One of the paths that is in $LOAD_PATH by default is '.', which is
    the current working directory.
    Logan Capaldo, Sep 5, 2006
    #5
    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. Sandra-24
    Replies:
    18
    Views:
    1,860
    Steve Holden
    Apr 29, 2006
  2. chandan
    Replies:
    2
    Views:
    768
    chandan
    Dec 3, 2007
  3. marekw2143
    Replies:
    3
    Views:
    1,362
    marekw2143
    Jul 25, 2009
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    374
    Martin P. Hellwig
    Mar 26, 2010
  5. Raj Singh
    Replies:
    2
    Views:
    196
    Rick DeNatale
    May 29, 2008
Loading...

Share This Page