# Gets gets

**URL:** <https://www.ruby-forum.com/t/gets-gets/90133>\
**Category:** Ruby\
**Created:** [March 26, 2007, 10:18am UTC](https://www.ruby-forum.com/t/gets-gets/90133 "2007-03-26T10:18:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [March 26, 2007, 10:18am UTC](https://www.ruby-forum.com/t/gets-gets/90133/1 "2007-03-26T10:18:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [March 26, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/gets-gets/90133/2 "2007-03-26T17:25:36Z")

</div>

On Mar 26, 2007, at 3:17 AM, John J. 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 G. II

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [March 26, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/gets-gets/90133/3 "2007-03-26T18:01:42Z")

</div>

Excellent explanation!  
Perfectly clear.
