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

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

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

···

On Sep 4, 2006, at 7:21 PM, brian.kejser@protexis.com wrote:

Thanks

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

···

On 9/4/06, brian.kejser@protexis.com <brian.kejser@protexis.com> wrote:

Hi

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

···

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

On 9/4/06, brian.kejser@protexis.com <brian.kejser@protexis.com> 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

Hi

Thanks.

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

···

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

On Sep 4, 2006, at 7:21 PM, brian.kejser@protexis.com 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

Hi

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

require 'one' will load the entire one.rb file.

···

On 9/4/06, brian.kejser@protexis.com <brian.kejser@protexis.com> wrote:

-----Original Message-----

From: "Michael Guterl" <mguterl@gmail.com>
Sent: Mon, September 4, 2006 16:45
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Subject: Re: How do I create an instance of a class in another rb file

On 9/4/06, brian.kejser@protexis.com <brian.kejser@protexis.com> 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

powerbook:~ michaelguterl$ ri require
--------------------------------------------------------- Kernel#require
     require(string) => true or false

···

On 9/4/06, brian.kejser@protexis.com <brian.kejser@protexis.com> wrote:

Hi

Thanks.

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

------------------------------------------------------------------------
     Ruby tries to load the library named _string_, returning +true+ if
     successful. If the filename does not resolve to an absolute path,
     it will be searched for in the directories listed in +$:+. If the
     file has the extension ``.rb'', it is loaded as a source file; if
     the extension is ``.so'', ``.o'', or ``.dll'', or whatever the
     default shared library extension is on the current platform, Ruby
     loads the shared library as a Ruby extension. Otherwise, Ruby tries
     adding ``.rb'', ``.so'', and so on to the name. The name of the
     loaded feature is added to the array in +$"+. A feature will not be
     loaded if it's name already appears in +$"+. However, the file name
     is not converted to an absolute path, so that ``+require
     'a';require './a'+'' will load +a.rb+ twice.

        require "my-library.rb"
        require
"db-driver"

-----Original Message-----

From: "Logan Capaldo" <logancapaldo@gmail.com>
Sent: Mon, September 4, 2006 16:33
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Subject: Re: How do I create an instance of a class in another rb file

On Sep 4, 2006, at 7:21 PM, brian.kejser@protexis.com 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
>

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.

···

On Sep 4, 2006, at 8:15 PM, brian.kejser@protexis.com wrote:

Hi

Thanks.

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