On Dec 11, 2006, at 7:02 AM, Pedro Fortuny Ayuso wrote:

> I would do somethink like:
>
> a = File.open("name_of_file")
> whole_file = a.inject("") {|s,e| s+e}
> a.close

That's a long way to say:

whole_file = File.read("name_of_file")

> However, if you are doing stuff *line by line* you had better do  
> sth like
>
> a = File.open("name_of_file")
> a.each do |line|
>  YOUR STUFF WITH line
> end
> a.close

File.foreach("name_of_file") do |line|
   # ... use line here
end

> For example, you want to count the number of lines where the word  
> "foo"
> appears
>
> a = File.open("myfile")
> count = 0
> a.each do |line|
>  count += 1 if line =~ /foo/
> end
> a.close
>
> count has now that value.

count = 0
File.foreach("myfile") do |line|
   count += 1 if line =~ /foo/
end

James Edward Gray II