Forum: Ruby Class args

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9f8ef676e9b0095de0c2ec36a0481a30?d=identicon&s=25 Stuart Brand (stuartbrand)
on 2006-06-05 00:50
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
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 Kirk Haines (Guest)
on 2006-06-05 01:09
(Received via mailing list)
On Sunday 04 June 2006 4:50 pm, Stuart Brand 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 Haines
9f8ef676e9b0095de0c2ec36a0481a30?d=identicon&s=25 Stuart Brand (stuartbrand)
on 2006-06-05 01:43
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-06-05 02:32
(Received via mailing list)
On Jun 4, 2006, at 5:50 PM, Stuart Brand 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.

2.  ruby_method_naming_style vs. javaMethodNamingStyle  ;)

James Edward Gray II
9f8ef676e9b0095de0c2ec36a0481a30?d=identicon&s=25 Stuart Brand (stuartbrand)
on 2006-06-05 14:02
Hi all

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

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
This topic is locked and can not be replied to.