Prev: Speeding up TCP
Next: A regexp?
From: Robert Klemme on 22 Mar 2010 12:48 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 23 Mar 2010 07:52 Jonathan Nielsen wrote: >>Thank you , appreciate it. -- Posted via http://www.ruby-forum.com/.
From: Hawksury Gear on 4 Apr 2010 09:06 > 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 4 Apr 2010 10:12 [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 4 Apr 2010 10:16
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. |