Gets gets

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

···

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

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