# No method error (because of unknown variable type)

**URL:** <https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960>\
**Category:** Ruby\
**Created:** [December 22, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960 "2007-12-22T18:35:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [December 22, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960/1 "2007-12-22T18:35:22Z")

</div>

Hi !

Here is a little example:

file “Functions.rb”:  
#---------------------  
def func(file)  
if !file.eof then #do something.  
end  
#---------------

file “main.rb”:

require ‘Functions’  
file\_1=open(‘bla.txt’,‘w’)  
func(file\_1)  
file\_1.close

I get an error message that eof method is undefined.  
I presume that this is because ruby does not know that the “file”  
variable  
is going to become a File.object.  
How do I solve this ?

Thanks.

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [December 22, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960/2 "2007-12-22T18:52:20Z")

</div>

Haris B. wrote:

> I get an error message that eof method is undefined.  
> I presume that this is because ruby does not know that the “file” variable  
> is going to become a File.object.  
> How do I solve this ?

Spell it “eof?”, not “eof”.

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [December 22, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960/3 "2007-12-22T18:56:28Z")

</div>

Hi Haris, the problem here is that the eof or the eof? methods must be  
used with files open as readeble like file\_1( ‘bla.txt’, ‘w’ ).  
If you are writing a file the eof is noy defined cuz you are always  
adding data to the end of it.  
The evaluation of the object class is made on the fly, ruby doesn’t try  
to predict the future objects classes.

Eduardo, Gorosito

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [December 22, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960/4 "2007-12-22T19:07:37Z")

</div>

Sebastian H. wrote:

> object (because that’s what was passed into the method) - that’s not the  
> problem. The problem is that File objects don’t have an eof method. It’s eof?  
> (with a question mark).
> 
> HTH,  
> Sebastian

I insist, is not a problem of eof or eof?, they are both defined. The  
problem is that he is calling eof for a IO open to write but not to  
read.

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [December 22, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960/5 "2007-12-22T19:14:03Z")

</div>

Decurnex Gorosito, Roberto Eduardo wrote:

> I insist, is not a problem of eof or eof?, they are both defined. The  
> problem is that he is calling eof for a IO open to write but not to read.

Okay, I was wrong that the eof method doesn’t exist. But if you have a  
File  
object opened for writing you still don’t get a NoMethodError that eof  
doesn’t exist. You get an IOError.  
So either something else is going on or the OP was wrong about the kind  
of  
error he was getting.

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [December 22, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960/6 "2007-12-22T19:01:10Z")

</div>

Haris B. wrote:

> I get an error message that eof method is undefined.  
> I presume that this is because ruby does not know that the “file” variable  
> is going to become a File.object.

You assume you get the error message when the method is defined. This is  
not  
true. The error is raised when the method is actually called (with a  
file  
object as a parameter). At this point ruby already knows that file is a  
File  
object (because that’s what was passed into the method) - that’s not the  
problem. The problem is that File objects don’t have an eof method. It’s  
eof?  
(with a question mark).

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [December 22, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960/7 "2007-12-22T20:05:41Z")

</div>

Problem solved !

It’s because the file was opened as ‘w’ and not ‘r’. I firstly created  
file  
and put some data into it and then I forgot to close it and reopen it as  
‘r’.  
It was strange to me that ruby would check for variable types in that  
way  
but obviously I was assuming wrong.

Thanks.

“Haris B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:fkjhnd$c6f$removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [December 24, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/no-method-error-because-of-unknown-variable-type/123960/8 "2007-12-24T23:30:34Z")

</div>

On Dec 22, 1:00 pm, “Haris B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “Haris B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message
> 
> > if !file.eof then #do something.  
> > I get an error message that eof method is undefined.  
> > I presume that this is because ruby does not know that the “file” variable  
> > is going to become a File.object.  
> > How do I solve this ?
> 
> > Thanks.

You can use ‘r+’ as the open mode if you want reading and writing  
(rather than closing and reopening the file handle or using  
IO#reopen). See `ri IO` (near the middle) for a list of available open  
modes.

Regards,  
Jordan
