Return of gets gets

Discussion in 'Ruby' started by John Joyce, Apr 23, 2007.

  1. John Joyce

    John Joyce Guest

    Not long ago I posted about
    gets gets

    I said it acted a bit like a Heredoc.
    I've since realized that it is actually more similar to
    readlines
    In fact, it behaves identically thus far.
    example:
    irb(main):004:0> lines = readlines
    ff
    fff
    akenll329
    \n
    kls
    => ["ff\n", "fff\n", "akenll329\n", "\\n\n", "kls\n"]
    irb(main):005:0> puts lines
    ff
    fff
    akenll329
    \n
    kls
    => nil
    irb(main):006:0> lines = gets gets
    fff
    ff
    ahoy
    \n
    lll ll
    => "ff\nahoy\n\\n\nlll ll\n"
    irb(main):007:0> puts lines
    ff
    ahoy
    \n
    lll ll
    => nil


    Now if you create a file such as:
    putsgetsgets.rb

    containing only:
    puts gets gets

    Then run the file, passing it some other file's text:
    ruby putsgetsgets.rb < otherfile.whatever

    It will output the same same as if you create a file:
    putsreadlines.rb

    Containing:
    puts readlines

    Then do the same:
    ruby putsreadlines.rb < otherfile.whatever

    The difference is of course it is not a developed method like
    readlines. Thus, it doesn't respond quite the same to methods such as
    length.
    readlines.length will return the number of lines.
    gets gets.length will return the number of characters.
    John Joyce, Apr 23, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PvdK
    Replies:
    0
    Views:
    2,961
  2. Stefan Mueller
    Replies:
    5
    Views:
    499
    Steven Saunderson
    Jul 10, 2006
  3. Greenhorn
    Replies:
    15
    Views:
    783
    Keith Thompson
    Mar 6, 2005
  4. John Joyce

    gets gets

    John Joyce, Mar 26, 2007, in forum: Ruby
    Replies:
    2
    Views:
    325
    John Joyce
    Mar 26, 2007
  5. libsfan01
    Replies:
    5
    Views:
    224
    Jeff North
    Dec 20, 2006
Loading...

Share This Page