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

B

brian.kejser

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
 
L

Logan Capaldo

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
 
B

brian.kejser

Hi

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



-----Original Message-----
From: "Michael Guterl" <[email protected]>
Sent: Mon, September 4, 2006 16:45
To: "ruby-talk ML" <[email protected]>
Subject: Re: 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?


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

ThanksMichael Guterl
 
B

brian.kejser

Hi

Thanks.

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




-----Original Message-----
From: "Logan Capaldo" <[email protected]>
Sent: Mon, September 4, 2006 16:33
To: "ruby-talk ML" <[email protected]>
Subject: Re: 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?

require 'one'
one = One.new
 
L

Logan Capaldo

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top