[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: why won't ruby chomp for me?
From:       mike () stok ! co ! uk (Mike Stok)
Date:       2004-01-29 16:14:57
Message-ID: RiaSb.3979$ef.3112 () twister01 ! bloor ! is ! net ! cable ! rogers ! com
[Download RAW message or body]

In article <200401291519.i0TFJ8d15618@moulon.inra.fr>,
ts  <decoux@moulon.inra.fr> wrote:
>>>>>> "E" == Emmanuel Touzery <emmanuel.touzery@wanadoo.fr> writes:
>
>E>     I was wondering.. IIRC, Perl came up with this "\n" in the end of 
>E> line in gets etc because in Perl, "" is false. So, if you want to have:
>E> while (<>) {print $_;}
>E>     working, you needed that even empty lines won't be "false". So they 
>E> said that they will put the "\n" in the line, and problem is gone, nice 
>E> hack etc.
>
> Well, I know nothing in this strange P language but this is not really the 
> reason.
>
> while (<>) {} is in reality a shortcut for while(defined($_ = <>)) {} (it
> must exist an old version of this strange language where while($_ = <>) {}
> was different from while (<>) {})
>
> The reason is that if the file don't end with a newline, like you say ""
> is false but defined("") is true

The real reason is that it was possible in ancient perls to miss the
last line of a file iff the file ended line "blah\n0" (e.g. emacs can
write out lines without a trailing \n on the last line.)

Perl's notion of truth and the behaviour of <> meant that the last thing
read by <> would be '0' which is false (but defined); if there were a
trailing \n then the string "0\n" would be true.

>E>     but in ruby, "" is true, so i'm wondering... why did ruby take this 
>E> over from perl? i find myself many times forgetting that chomp and the 
>E> fact ruby offers me the "raw" line format never ever helped me in any 
>E> way. 
>
> Probably the good question is : what is a line ? do the "line" separator
> belong to the line or not ?

One problem with "autochomping" is that at the end of file you don't
know if the record (line) ended "properly" or whether the read ended
because of an end of file.

Depending on what you're doing you might just be able to make a class
which inherits everything from File and define your own readline

class MyFile < File
  def readline(*args)
    super.chomp
  end
end

Hope this helps,

Mike

-- 
mike@stok.co.uk                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA 
mike@exegenix.com                  | Fingerprint      0570 71CD 6790 7C28 3D60
http://www.exegenix.com/           |                  75D2 9EC4 C1C0 0599 13DA

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic