gets gets

J

John Joyce

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
 
J

James Edward Gray II

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
 
J

John Joyce

Excellent explanation!
Perfectly clear.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top