Re: using classes problem

From: Scott D_o

I am trying to use a class that I wrote in another file, and
it doesn’t
seem to want to work. At the beginning of the file that I do [require
‘classname’] with the single quotes

a) What is the name of the file that you put your class in?
b) Where is the file, relative to the file that has the “require” line
in it?

Gavin K. wrote:

From: Scott D_o

I am trying to use a class that I wrote in another file, and
it doesn’t
seem to want to work. At the beginning of the file that I do [require
‘classname’] with the single quotes

a) What is the name of the file that you put your class in?
b) Where is the file, relative to the file that has the “require” line
in it?

Ok so here is some code that is having the problem with the require. I
tested out some programs from rubyforge, and it gave me the same error.

TestClass two seperate files

class TestClass
def testMethod()
return “Hello I am in TestClass”
end
end


ClassTester

require ‘TestClass’

ob = TestClass.new()
puts(“Testing: #{ob.testMethod()}”)

Thanks

Ok so here is some code that is having the problem with the require. I
tested out some programs from rubyforge, and it gave me the same error.


ClassTester

require ‘TestClass’

ob = TestClass.new()
puts(“Testing: #{ob.testMethod()}”)

It works for me. Both the program files are in the same folder.

Here’s a slightly modified code (normally the () is not there after the
method name. We use lowercase characters for method names and _
character to
separate out the multi-words in a method name. I am also using
single-quotes
for plain strings (the first case in your program).

testclass.rb

class TestClass
def test_method
‘Hello I am in TestClass’
end
end

classtester.rb

require ‘testclass’
ob = TestClass.new
puts(“Testing: #{ob.test_method()}”)

The output -

ruby classtester.rb
Testing: Hello I am in TestClass
Exit code: 0

Hope this helps.

Satish T. wrote:

Ok so here is some code that is having the problem with the require. I
tested out some programs from rubyforge, and it gave me the same error.


ClassTester

require ‘TestClass’

ob = TestClass.new()
puts(“Testing: #{ob.testMethod()}”)

It works for me. Both the program files are in the same folder.

Here’s a slightly modified code (normally the () is not there after the
method name. We use lowercase characters for method names and _
character to
separate out the multi-words in a method name. I am also using
single-quotes
for plain strings (the first case in your program).

testclass.rb

class TestClass
def test_method
‘Hello I am in TestClass’
end
end

classtester.rb

require ‘testclass’
ob = TestClass.new
puts(“Testing: #{ob.test_method()}”)

The output -

ruby classtester.rb
Testing: Hello I am in TestClass
Exit code: 0

Hope this helps.

Nope it still gives me the same error. IDK what to do, cause I tried it
on two computers and they both give me the same one. On both computers I
installed ruby using the 1click installer, do you think this would have
any effect on it, or are there certain config files that I can check to
make sure everything is in order?
Thanks

Scott D_o wrote:

class TestClass
ob = TestClass.new()
puts(“Testing: #{ob.testMethod()}”)

You didn’t answer either of my questions.

What is the name of the FILE that you have your TestClass defined in?
It should be something like “testclass.rb”. That’s what you should put
in your require statement, e.g.
require ‘testclass.rb’

Also, unless you’re installing it somewhere in your require path, you
should have that file in the same directory as your ‘class tester’ file.

On 10/17/06, Scott D_o [email protected] wrote:

puts(“Testing: #{ob.testMethod()}”)

One more thing - under what name have you stored your require TestClass
program?

Satish T. wrote:

On 10/17/06, Scott D_o [email protected] wrote:

puts(“Testing: #{ob.testMethod()}”)

One more thing - under what name have you stored your require TestClass
program?

I have it named as TestClass.rb if that is what you are asking

Scott,

A couple of things, probably not the cause, but you seem to have some
of the naming conventions mixed…

assuming that both files are in the same directory, it is good practice
to not use cases in your file names, so I would code them;


testclass.rb

class TestClass
def testMethod()
return “Hello I am in TestClass”
end
end


classtester.rb

require ‘testclass’

ob = TestClass.new()
puts(“Testing: #{ob.testMethod()}”)

The require is the filename without the .rb extension.

James Mccarthy wrote:

Scott,

A couple of things, probably not the cause, but you seem to have some
of the naming conventions mixed…

assuming that both files are in the same directory, it is good practice
to not use cases in your file names, so I would code them;


testclass.rb

class TestClass
def testMethod()
return “Hello I am in TestClass”
end
end


classtester.rb

require ‘testclass’

ob = TestClass.new()
puts(“Testing: #{ob.testMethod()}”)

The require is the filename without the .rb extension.

No I used require ‘testclass’, and I tried running it in freeIDE, and
for some reason it actually ran, so I don’t know what the deal is when I
try to run it from console, it just gives me that rubygem custom require
error.

so you are doing that from rails console rather than irb?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs