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 Bogdanovic 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".

···

--
RMagick: http://rmagick.rubyforge.org/

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

Haris Bogdanovic wrote:

···

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

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

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 Bogdanovic" <fbogdanovic@xnet.hr> wrote in message
news:fkjhnd$c6f$1@garrison.globalnet.hr...

···

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.

Sebastian Hungerecker wrote:

Haris Bogdanovic 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
  
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.

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

···

On Dec 22, 1:00 pm, "Haris Bogdanovic" <fbogdano...@xnet.hr> wrote:

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 Bogdanovic" <fbogdano...@xnet.hr> wrote in message

news:fkjhnd$c6f$1@garrison.globalnet.hr...

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

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.

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826