Forum: Ruby Problem with ruby classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A8199b96ffa7b6caf08c808d835eea81?d=identicon&s=25 Manisha T. (manisha)
on 2009-02-02 17:23
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)
A3ce40111c76d0d40c3c23f4e18e7e35?d=identicon&s=25 Stefano C. (crocco)
on 2009-02-02 17:25
(Received via mailing list)
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
A8199b96ffa7b6caf08c808d835eea81?d=identicon&s=25 Manisha T. (manisha)
on 2009-02-02 17:34
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
Julian Leviston (Guest)
on 2009-02-03 03:30
(Received via mailing list)
Make sure you don't include the extension

Sent from my iPhone

On 03/02/2009, at 3:33 AM, Manisha Tripathy <pujari.manisha@gmail.com>
This topic is locked and can not be replied to.