Class args


#1

Whats wrong with this class?

class ReadFile
def ReadFile.openAndRead(arg)
f = File.open(arg, “r”)
yield f
f.close()
end
end

when it run I get a initialize’: No such file or directory -
thefileiwanttoopen

Any Ideas?

Thanks all


#2

On Sunday 04 June 2006 4:50 pm, Stuart B. wrote:

class ReadFile
def ReadFile.openAndRead(arg)
f = File.open(arg, “r”)
yield f
f.close()
end
end
when it run I get a initialize’: No such file or directory -
thefileiwanttoopen

It must relate to how you are using it, as there’s no error with the
code as
written. On the face, that just says to me that the arg that you are
passing
points somewhere where there is nothing to open.


class ReadFile
def ReadFile.openAndRead(arg)
f = File.open(arg, “r”)
yield f
f.close()
end
end

ReadFile.openAndRead(’/tmp/readfile.rb’) {|f| puts f.read}

ruby /tmp/readfile.rb

It should print the file back out to you.

A couple suggestions, though. First, most of the time you will probably
want
to use the block form of File.open. Also, read only mode is assumed if
no
mode is given, so you can simpify your code as:

file.open(arg) {|f| yield f}

Beyond that, you’re going to have to provide more context for us to help
much.

Kirk H.


#3

The problem is when I try to combine text

url = HtmlRead.clean("#{session.gets}")
urlResult = “/srv/www/htdocs/” + url[1].to_s
puts urlResult

the output is what it should be, however I get the error the the files
do not exist, if I put the file in manually it works, as in

ReadFile.openAndRead("/srv/www/htdocs/index.html) do |line|
session.print while line.gets
end

if I leave it as
ReadFile.openAndRead(urlResult) do |line|
session.print while line.gets
end

I get the error the the file does not exist even tho the puts said it
correctly, I think it must be in the way I am joining the text???

Thanks again


#4

On Jun 4, 2006, at 5:50 PM, Stuart B. wrote:

class ReadFile
def ReadFile.openAndRead(arg)

Not related to your problem, but two quick suggestions about the
above line:

  1. If you change the method definition to:

def self.name(…)

You won’t need to change it when you rename the class.

  1. ruby_method_naming_style vs. javaMethodNamingStyle :wink:

James Edward G. II


#5

Hi all

Thanks for your help, I have found the fault :slight_smile:

when splitting up the html request using .split it was adding a new line
character to the end of each part “\n” which was taken as part of the
file name, using strip to take this off has solved the problem

Thanks again all

Stuart