# Class args

**URL:** <https://www.ruby-forum.com/t/class-args/95519>\
**Category:** Ruby\
**Created:** [June 5, 2006, 12:50am UTC](https://www.ruby-forum.com/t/class-args/95519 "2006-06-05T00:50:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 5, 2006, 12:50am UTC](https://www.ruby-forum.com/t/class-args/95519/1 "2006-06-05T00:50:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 5, 2006, 1:09am UTC](https://www.ruby-forum.com/t/class-args/95519/2 "2006-06-05T01:09:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 5, 2006, 1:43am UTC](https://www.ruby-forum.com/t/class-args/95519/3 "2006-06-05T01:43:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 5, 2006, 2:32am UTC](https://www.ruby-forum.com/t/class-args/95519/4 "2006-06-05T02:32:03Z")

</div>

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 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 5, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/class-args/95519/5 "2006-06-05T14:02:27Z")

</div>

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
