Newbie question on file reading

Hi,
I have got the code to read a file line by line amd it works too but I
don't understand it.

fp = File.open(dataFile, "r")
fp.each_line do |line|
puts line
end

My question is: the "open" method is not mentioned in the doc of File
class
http://ruby-doc.org/core/classes/File.html
What type does it return?

My 2nd question is how can I read a file char by char. Basically the
file doesn't have line breaks.

Please help..

···

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

I believe it comes from the IO class instead
http://ruby-doc.org/core/classes/IO.html

How big is your file? IO.read("/path/to/file") will read the entire
file into a string. If you really want to read it character by
character

File.open("/path/to/file") do |f|
  while true do
    begin
      a = f.readchar
      # do something with a
    rescue EOFError
      break
    end
  end
end

martin

···

On Tue, Aug 24, 2010 at 4:34 PM, Rajarshi Chakravarty <raj_plays@yahoo.com> wrote:

My 2nd question is how can I read a file char by char. Basically the
file doesn't have line breaks.

Yes, this is true.

Rajarshi, you can make your life a bit simpler by doing

File.foreach(dataFile) do |line|
  puts line
end

If you actually need to open the file and manipulate the File / IO
object directly (e.g. because you want to rewind), you can use the
block form of File.open (or IO.open):

File.open(dataFile, "r") do |fp|
  fp.each_line do |line|
    puts line
  end
end

This ensures the file is properly closed which was not the case in
your original example. You can find more at

http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html

And if you want to know the internal workings you can look here

http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html

Kind regards

robert

···

2010/8/24 Peter Hickman <peterhickman386@googlemail.com>:

I believe it comes from the IO class instead
class IO - RDoc Documentation

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

Thank you, Robert.
The links were most helpful.

I need to read char substrings from a file that has no line breaks in
it.
Is the code below the best way to do it?

File.foreach(dataFile) do |line|
  puts line
end

Will the "line" var give the entire text in the file?

Another question (this is posted on the link you gave)
Instead of doing File.foreach(dataFile) can I not do
IO.foreach(dataFile) do |line|?

···

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

Thank you, Robert.

Your welcome!

The links were most helpful.

Good!

I need to read char substrings from a file that has no line breaks in
it.

Note that you can use a different separator character:

File.foreach data_file, "SEPARATOR" do |entry|
  puts entry
end

The same works for File.each:

File.open... do |io|
  io.each "SEPARATOR" do |entry|
    ...
  end
end

Is the code below the best way to do it?

File.foreach(dataFile) do |line|
puts line
end

Will the "line" var give the entire text in the file?

No, because this will try to load lines separated by \n. If you want
the whole text of the file in one go you can do

contents = File.read data_file

However, if the file is large, a blocked approach usually yields better results:

File.open data_file do |io|
  buffer = ""

  while io.read(1024, buffer)
    buffer.each_char {|ch| p ch} # note, ch is really a String of length 1
  end
end

Another question (this is posted on the link you gave)
Instead of doing File.foreach(dataFile) can I not do
IO.foreach(dataFile) do |line|?

Yes, File and IO are interchangeable here. Personally I prefer to use
File when dealing with files just for documentation reasons.

Kind regards

robert

···

2010/8/24 Rajarshi Chakravarty <raj_plays@yahoo.com>:

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

Thank you, Martin and Robert

···

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

In continuation with my questions about reading files from the file
system...

It seems Dir[File.join(root, subDir, "*")] gives the full path name of
all files in the sub directory

Can I modify the expression to give just the basename?

···

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

You can feed the result through #map and File.basename.

Cheers

robert

···

2010/8/26 Rajarshi Chakravarty <raj_plays@yahoo.com>:

In continuation with my questions about reading files from the file
system...

It seems Dir[File.join(root, subDir, "*")] gives the full path name of
all files in the sub directory

Can I modify the expression to give just the basename?

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