why won't ruby chomp for me?

E

Emmanuel Touzery

Hello,

I was wondering.. IIRC, Perl came up with this "\n" in the end of
line in gets etc because in Perl, "" is false. So, if you want to have:
while (<>) {print $_;}
working, you needed that even empty lines won't be "false". So they
said that they will put the "\n" in the line, and problem is gone, nice
hack etc.

but in ruby, "" is true, so i'm wondering... why did ruby take this
over from perl? i find myself many times forgetting that chomp and the
fact ruby offers me the "raw" line format never ever helped me in any
way. is it just historical praise to Perl?

just curious,

emmanuel
 
T

ts

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

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 ?


Guy Decoux
 
S

Steven Jenkins

ts said:
Probably the good question is : what is a line ? do the "line" separator
belong to the line or not ?

I suppose any language can define it any way. But it seems clear that
much of the Ruby library is modeled after C and Unix (which are defined
by international standards). In ISO C, the newline is removed from the
string returned by gets().

On the other hand, C doesn't let you redefine the input line separator.
Making one language comply with another language's specification is tricky.

Steve
 
M

Mike Stok

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
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
 
E

Emmanuel Touzery

Hello,
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.

so this is why so many tools insist on a final carriage return at the end of a
file? in the case of a truncated file? i never quite got it :O)
In your example above, if the newline was removed by <>, would you
expect the print to put it back? But print doesn't do that, and
shouldn't, what if you want to do two prints to build up a single line!

i'm satisfied of the distinction between puts and print :O)

generally, i think it's a good design decision, but i have a bit the feeling
the particular case killed the general case (when you don't care about that).
but i agree that uniformity pays off, and this is definitely very good that
it's not "half of the methods give the \n, half don't". and since sometimes
it's needed...

thanks for all the very clear answers :O)

PS: somehow i many times though that Guy meant "Pascal" when he mentionned
"the P language" ;O)
 
D

Dion Mendel

i find myself many times forgetting that chomp and the
fact ruby offers me the "raw" line format never ever helped me in any
way. is it just historical praise to Perl?

I too find the chomp an ugly perlism. Has anyone written code that
makes use of the trailing line separator from IO#gets?

Unless someone argues why it would not be a good idea, I will submit an
RCR for changing IO#gets to not include the trailing line separator by
default.

Dion.
 
N

nobu.nokada

Hi,

At Fri, 30 Jan 2004 10:07:49 +0900,
Dion said:
I too find the chomp an ugly perlism. Has anyone written code that
makes use of the trailing line separator from IO#gets?

It should be another method, I guess.
 
A

Ara.T.Howard

Date: Fri, 30 Jan 2004 10:07:49 +0900
From: Dion Mendel <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: why won't ruby chomp for me?



I too find the chomp an ugly perlism. Has anyone written code that makes
use of the trailing line separator from IO#gets?

Unless someone argues why it would not be a good idea, I will submit an RCR
for changing IO#gets to not include the trailing line separator by default.

it might not be agood idea when lines are empty... but i suppose it would
work. i would loath having to remember to open a file in binary mode in order
for methods in IO to _not_ to automagical things to the data too...

2 cts.

-a

--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top