gets gets

Discussion in 'Ruby' started by John Joyce, Mar 26, 2007.

  1. John Joyce

    John Joyce Guest

    I'm a little surprised at this.
    In irb, I tried puts gets gets.
    Why? I don't know.
    but basically, gets gets, seems to almost act like a heredoc!
    myString = gets gets
    puts myString
    John Joyce, Mar 26, 2007
    #1
    1. Advertising

  2. On Mar 26, 2007, at 3:17 AM, John Joyce wrote:

    > I'm a little surprised at this.
    > In irb, I tried puts gets gets.
    > Why? I don't know.
    > but basically, gets gets, seems to almost act like a heredoc!
    > myString = gets gets
    > puts myString


    It's not surprising at all. First, let's insert the parentheses so
    we can see it as Ruby does:

    puts(gets(gets()))

    Now we see that the right-most gets() call must be resolved first.
    Assuming you enter the String "END", that will be passed to the left-
    most gets() call as a parameter.

    The parameter to gets() is an input record divider, so it will read
    ahead until it encounters "END" instead of the traditional "\n".
    This is the heredoc-like behavior you are seeing.

    The result of the second gets() is then printed by puts().

    Hope that explains what you are seeing.

    James Edward Gray II
    James Edward Gray II, Mar 26, 2007
    #2
    1. Advertising

  3. John Joyce

    John Joyce Guest

    Excellent explanation!
    Perfectly clear.

    On Mar 27, 2007, at 12:24 AM, James Edward Gray II wrote:

    > On Mar 26, 2007, at 3:17 AM, John Joyce wrote:
    >
    >> I'm a little surprised at this.
    >> In irb, I tried puts gets gets.
    >> Why? I don't know.
    >> but basically, gets gets, seems to almost act like a heredoc!
    >> myString = gets gets
    >> puts myString

    >
    > It's not surprising at all. First, let's insert the parentheses so
    > we can see it as Ruby does:
    >
    > puts(gets(gets()))
    >
    > Now we see that the right-most gets() call must be resolved first.
    > Assuming you enter the String "END", that will be passed to the
    > left-most gets() call as a parameter.
    >
    > The parameter to gets() is an input record divider, so it will read
    > ahead until it encounters "END" instead of the traditional "\n".
    > This is the heredoc-like behavior you are seeing.
    >
    > The result of the second gets() is then printed by puts().
    >
    > Hope that explains what you are seeing.
    >
    > James Edward Gray II
    >
    >
    John Joyce, Mar 26, 2007
    #3
    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. Bertus Dam
    Replies:
    5
    Views:
    847
    Bertus Dam
    Aug 29, 2003
  2. Steve Donnelly
    Replies:
    0
    Views:
    2,063
    Steve Donnelly
    Aug 30, 2003
  3. Stefan Mueller
    Replies:
    5
    Views:
    505
    Steven Saunderson
    Jul 10, 2006
  4. John Joyce

    Return of gets gets

    John Joyce, Apr 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    178
    John Joyce
    Apr 23, 2007
  5. libsfan01
    Replies:
    5
    Views:
    228
    Jeff North
    Dec 20, 2006
Loading...

Share This Page