Inconsistency with IO.readlines

J

Justin Rudd

I've noticed a slight inconsistancy with IO.readlines depending on the
line ending. If the line ending is PC (\r\n) or Unix (\n) then it
works fine no matter the platform. But if the line ending is Mac
(\r), IO.readlines returns one line no matter how many lines it is
supposed to be.

Maybe this isn't such a big deal anymore since Mac OS X uses Unix line
endings, but believe it or not I've got files coming in from Mac OS 9
users.

Anyone else seen this? I'm using a build built from the last stable
snapshot under Cygwin.
 
G

gene.tani

Hmm, don't know OS9 file format, but couldn't you set $/
(==$INPUT_RECORD_SEPARATOR) to last character in the file, something
like that?) Don't think there''s anything like Python universal
newlines (open file in mode "U").
 
J

James Edward Gray II

Let me apologize in advance, because I've not bee following this
thread. I have been working with Gavin Sinclair to document
delegate.rb though, and together we've been hard pressed to find a
single good use for SimpleDelegate...

# ========== ext/input_reader.rb ==========
# ext/input_reader.rb
# accept some name argument. if name is nil || '-',
# then use $stdin and send that to the block
# otherwise, use File.open(name, *restargs)

require 'delegate'

def input_reader(fname, *fargs)
block = Proc.new {|source|
getter = SimpleDelegator.new(source)
yield getter
getter.__setobj__(nil)
}

if fname.nil? || fname == '-' then
block[$stdin]
else
File.open(fname, *fargs) {|f| block[f]}
end
end

Would you mind explaining to me why you use SimpleDelegate above? I
would really appreciate it.

James Edward Gray II
 
J

James Edward Gray II

My use of SimpleDelegator is that I don't want a case like this:
handle = nil
input_reader("myfile") {|handle| ...}
some_func(handle)

Thank you for walking through this with me.
granted, the file ensures that things are closed off so I really don't
have to, but to go along with the idea of how things `should be' - I
used simple delegator for __set_obj__. Just design philosophy.

The only case I can think of for this mattering in the file I gave is
this:

handle = nil
input_reader(some_arg) {|handle| ...}
handle.puts "Meow Mix Please Deliver"

Hmm, but the code I saw was:
block = Proc.new {|source|
getter = SimpleDelegator.new(source)
yield getter
getter.__setobj__(nil)
}

You're worried that "getter" may have existed outside input_reader(),
in the calling code, and you want it to be immediately obvious if you
trample that value?

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top