Professional Web Applications Themes

why won't ruby chomp for me? - Ruby

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

  1. #1

    Default why won't ruby chomp for me?

    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



    Emmanuel Guest

  2. #2

    Default Re: why won't ruby chomp for me?

    >>>>> "E" == Emmanuel Touzery <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

    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





    ts Guest

  3. #3

    Default Re: why won't ruby chomp for me?

    ts wrote: 

    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



    Steven Guest

  4. #4

    Default Re: why won't ruby chomp for me?

    In article <inra.fr>,
    ts <inra.fr> wrote: [/ref]
    >
    >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[/ref]

    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.
     

    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

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

  5. #5

    Default Re: why won't ruby chomp for me?

    Hello,
     

    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)
     

    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)


    Emmanuel Guest

  6. #6

    Default Re: why won't ruby chomp for me?

    On Fri, Jan 30, 2004 at 12:04:30AM +0900, Emmanuel Touzery wrote:
     

    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.


    Dion Guest

  7. #7

    Default Re: why won't ruby chomp for me?

    Hi,

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

    It should be another method, I guess.

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  8. #8

    Default Re: why won't ruby chomp for me?

    On Fri, 30 Jan 2004, Dion Mendel wrote:
     
    >
    > 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.[/ref]

    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'
    ================================================== =============================

    Ara.T.Howard Guest

Similar Threads

  1. newbie question about chomp...
    By R in forum PERL Beginners
    Replies: 2
    Last Post: February 23rd, 07:21 AM
  2. Return value of chomp, with alternate value in $/
    By David Oswald in forum PERL Miscellaneous
    Replies: 6
    Last Post: August 9th, 03:18 AM
  3. Replies: 1
    Last Post: August 6th, 03:58 PM
  4. chomp (filecontents = <HANDLE>); slows down
    By Villy Kruse in forum PERL Miscellaneous
    Replies: 1
    Last Post: June 24th, 09:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139