hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Double Gets as Heredoc #

by why in inspect

From John Joyce in [ruby-talk:245343]:

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!

Such a simple and unintended thing: the double-gets. Do you see how it works?

said on 26 Mar 2007 at 10:24
Maybe this will help:

irb > puts(inner=gets(outer=gets))
STOP
This is text inside our pseudo heredoc
STOP
irb > p outer
"STOP\n" 
irb > p inner
"This is the text inside our pseudo heredoc\nSTOP\n" 

Basically, the innermost gets takes a separator argument as the input from the outermost gets .

Proof:


irb > puts(inner=gets("STOP"))
I feel so clever right now, I'm probably glowing.
STOP
irb > p inner
"I feel so clever right now, I'm probably glowing.\nSTOP\n"
said on 26 Mar 2007 at 11:15

The first gets gets the gets seperator while the second gets gets the text using the gets serperator.

Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.

said on 26 Mar 2007 at 11:47

Now that is really neat!

said on 26 Mar 2007 at 12:10

I like gets puts too.


irb(main):009:0> gets puts

a
s
f
d
=> "a\ns\nf\nd\n" 

But otherwise, thats pretty cool.

said on 26 Mar 2007 at 12:49

Hank: I don’t see what gets puts does?

said on 26 Mar 2007 at 15:46
gets puts is the same as gets nil since puts without any parameters returns nil. Thus, there isn’t any separator.
said on 27 Mar 2007 at 06:11

Hank: you didn’t warn me that you needed to CTRL +D get close the input there. You just ate my o`o!!! [I dunno what my o`o is though.]

said on 27 Mar 2007 at 20:20

RSL , that sounds like a personal problem.

said on 28 Mar 2007 at 06:52

puts gets ferrets that has hats with stats

said on 28 Mar 2007 at 13:47

wow :) heredocs, indeed.

puts gets(delimiter = gets).chomp(delimiter)
said on 30 Mar 2007 at 10:34

nothing to do with this problem: didn’t you have a webpage with programms that could execute the page, ähm the page was actually a programm. omy this lost link is like a hole in my head. or did i dream or something…

said on 01 Apr 2007 at 02:12

Thanks to James Edward Gray II ’s great explanation, it made perfect sense. I also tried subsequent oddities like gets puts and stuff. Ruby is fun to screw with even when you don’t fully know what you’re doing! Not many languages are as easy to screw around with. I just with unicode was native in Ruby so that characters beyond ascii would behave more predicably and less surprisingly…. Oh and _Why? You’re just very Austin/San Marcos, Texas. Those vibes are running strong in the Ruby community so that’s pretty freaky to some folks coming at it with hardened years of other stuff.

said on 01 Apr 2007 at 09:15
Oh 1 more thing,

gets gets

isn’t gets gets without puts.

puts gets gets

Heck this could easily make a flexible little goofy tool with a prompt for interactivity or simply as a tool to request input. Only problem is it’s dangerous if exposed to the unknown/untrusted outside world. overflow city? Needs some checking for data size, and something to prevent any further injection of code, in case the text goes somewhere that it might matter! (I wish I knew as much about programming as ya’ll do, then I’d be able to use this stuff to do more stuff)
11 Jul 2010 at 20:36

* do fancy stuff in your comment.

PREVIEW PANE