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

**URL:** <https://www.ruby-forum.com/t/what-is-the-syntax-for-passing-a-file-to-a-class/192505>\
**Category:** Ruby\
**Created:** [July 31, 2010, 8:30pm UTC](https://www.ruby-forum.com/t/what-is-the-syntax-for-passing-a-file-to-a-class/192505 "2010-07-31T20:30:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodfriend](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@goodfriend](https://www.ruby-forum.com/u/goodfriend)\
**Post date:** [July 31, 2010, 8:30pm UTC](https://www.ruby-forum.com/t/what-is-the-syntax-for-passing-a-file-to-a-class/192505/1 "2010-07-31T20:30:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![goodfriend](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@goodfriend](https://www.ruby-forum.com/u/goodfriend)\
**Post date:** [July 31, 2010, 9:02pm UTC](https://www.ruby-forum.com/t/what-is-the-syntax-for-passing-a-file-to-a-class/192505/2 "2010-07-31T21:02:21Z")

</div>

Frank G. wrote:

> 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?
