What is the syntax for passing a File to a Class?

F

Frank Guerino

Hi,

I have a log file that I've created using the simple command:

logFile = File.new("../LOGS/IF4IT.log", "w")

I can easily write to it at a global level using syntax like:

logFile.puts 'Write this string to logFile.'

I'd like to pass the log file into a Class I've defined so I can print
to the logFile from inside Class methods. I try to pass in the log file
like I would any other variable but I get error messages. Is there a
specific syntax that I should be using?

class MyClass
def initialize stringOne, stringTwo, logFile
...
end
end

The above class yields errors when I try to invoke it using:

newMyClass = MyClass.new stringA, stringB, logFile

I've tried to find documentation that talks about passing Files as
arguments to methods and to classes but can't seem to find anything
obvious. Any thoughts?

Thanks for your help, in advance!

Frank
 
P

Paul Harrington

Frank said:
I'd like to pass the log file into a Class I've defined so I can print
to the logFile from inside Class methods. I try to pass in the log file
like I would any other variable but I get error messages. Is there a
specific syntax that I should be using?

class MyClass
def initialize stringOne, stringTwo, logFile
...
end
end

The above class yields errors when I try to invoke it using:

newMyClass = MyClass.new stringA, stringB, logFile

I've tried to find documentation that talks about passing Files as
arguments to methods and to classes but can't seem to find anything
obvious. Any thoughts?

Thanks for your help, in advance!

Frank

logFile is a just a regular variable like everything else, there's no
special syntax for using it just because it refers to a File object.
Your assignment should work; whats the actual error message and the
actual code that causes it?
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top