From: Robert Klemme on
2010/3/22 jbw <jbw(a)jbw.cc>:
> Also might want to check if it is a file and skip directories:
>
> puts Dir["/*"].map { |f| if(!File.directory?(f)) then File.read f end }

Good point!

Dir["dir/*"].each{|f| test ?f,f and puts File.read(f)}

:-)

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From: Hawksury Gear on
Jonathan Nielsen wrote:
>>Thank you , appreciate it.

--
Posted via http://www.ruby-forum.com/.

From: Hawksury Gear on
> If it is only for output purposes, we can actually do it in one line:
>
> puts Dir["K:/test/*"].map {|f| File.read f}
>
> Note: this is not necessarily efficient nor safe (just think of 3GB
> files...).
>
> Kind regards
>
> robert

Many Thanks for replying robert, I am trying to print the contents of
each file
in the following way,It isn't giving me any error message but it is not
showing any output, Could you please help.

arr= Dir.open("K:/test").entries
arr.each do| file |
File.open("file","a+") do |fd|
fd.each {|line| print line}
end

Regards,
Gear
--
Posted via http://www.ruby-forum.com/.

From: Aurélien AMILIN on
[Note: parts of this message were removed to make it a legal post.]

First you should use file wich contain the filename of your file instead of
"file" which is a string
Then try to open your file with the r option not a+ (a+ means you place the
cursor at the end of the file to append some content while r is for reading
so the cursor is placed at the beginning of the file) so :

File.open(file,"r")
instead of :
File.open("file","a+")


2010/4/4 Hawksury Gear <blackhawk_932(a)hotmail.com>

> > If it is only for output purposes, we can actually do it in one line:
> >
> > puts Dir["K:/test/*"].map {|f| File.read f}
> >
> > Note: this is not necessarily efficient nor safe (just think of 3GB
> > files...).
> >
> > Kind regards
> >
> > robert
>
> Many Thanks for replying robert, I am trying to print the contents of
> each file
> in the following way,It isn't giving me any error message but it is not
> showing any output, Could you please help.
>
> arr= Dir.open("K:/test").entries
> arr.each do| file |
> File.open("file","a+") do |fd|
> fd.each {|line| print line}
> end
>
> Regards,
> Gear
> --
> Posted via http://www.ruby-forum.com/.
>
>

From: Jesús Gabriel y Galán on
On Sun, Apr 4, 2010 at 3:06 PM, Hawksury Gear <blackhawk_932(a)hotmail.com> wrote:
>> If it is only for output purposes, we can actually do it in one line:
>>
>> puts Dir["K:/test/*"].map {|f| File.read f}
>>
>> Note: this is not necessarily efficient nor safe (just think of 3GB
>> files...).
>>
>> Kind regards
>>
>> robert
>
> Many Thanks for replying robert, I am trying to print the contents of
> each file
> in the following way,It isn't giving me any error message but it is not
> showing any output, Could you please help.
>
>  arr= Dir.open("K:/test").entries
>  arr.each do| file |
>      File.open("file","a+") do |fd|
>        fd.each {|line| print line}
>        end
>

You have two errors there. "file" is a literal string, not the
contents of the variable file. Use file without quotes.

The second:

http://ruby-doc.org/core/classes/IO.html

Take a look at the documentation of the open modes:

"a+" | Read-write, starts at end of file if file exists,
| otherwise creates a new file for reading and
| writing.


Starts at the end of the file, so there's nothing else to read...
You probably want this instead:

File.open(file, "r") do |file|
...
end

Jesus.

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Speeding up TCP
Next: A regexp?