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
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

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

Now that is really neat!

said on

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

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

said on
gets puts is the same as gets nil since puts without any parameters returns nil. Thus, there isn’t any separator.
said on

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

RSL , that sounds like a personal problem.

said on

puts gets ferrets that has hats with stats

said on

wow :) heredocs, indeed.

puts gets(delimiter = gets).chomp(delimiter)
said on

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

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
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)
said on DD Mon YYYY at HH:MM

* do fancy stuff in your comment.

PREVIEW PANE