no method error (because of unknown variable type)

H

Haris Bogdanovic

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

Tim Hunter

Haris said:
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".
 
D

Decurnex Gorosito, Roberto Eduardo

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
 
S

Sebastian Hungerecker

Haris said:
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
 
D

Decurnex Gorosito, Roberto Eduardo

Sebastian said:
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

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

Sebastian Hungerecker

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

Haris Bogdanovic

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

MonkeeSage

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.

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
 

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

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top