No method error (because of unknown variable type)

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.

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”.

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

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.

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.

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

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.” [email protected] wrote in message
news:[email protected]

On Dec 22, 1:00 pm, “Haris B.” [email protected] wrote:

“Haris B.” [email protected] 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

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