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

On Sep 4, 2006, at 7:21 PM, [email protected] wrote:

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

require ‘one’
one = One.new

On 9/4/06, [email protected] [email protected] 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 G.

Hi

Thanks.

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

Hi

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

On 9/4/06, [email protected] [email protected] wrote:

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, [email protected] [email protected] wrote:

Hi

Thanks.

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

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

 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”

On Sep 4, 2006, at 8:15 PM, [email protected] 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.