# Lambda funcs that call gets won't halt program execution?

**URL:** <https://www.ruby-forum.com/t/lambda-funcs-that-call-gets-wont-halt-program-execution/122256>\
**Category:** Ruby\
**Created:** [December 5, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/lambda-funcs-that-call-gets-wont-halt-program-execution/122256 "2007-12-05T23:30:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [December 5, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/lambda-funcs-that-call-gets-wont-halt-program-execution/122256/1 "2007-12-05T23:30:11Z")

</div>

Once again, a problem with my Befunge interpreter. I’m trying to  
implement the ~ and & functions - get a character and a number,  
respectively. My & function looks like this:  
instructions["&"] = lambda { print "Number: "; stack.push  
gets().strip.to\_i}

But it complains about gets() being nil. as soon as the function is run.  
Well, of course it is. It has let me type. Is there anyway to make it  
halt?

Why does Ruby keep picking on me? Oh, wait. The principle of least  
surprise - all programming languages infuriate me, therefore it would  
surprise me least if Ruby did too. 🙂

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [December 5, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/lambda-funcs-that-call-gets-wont-halt-program-execution/122256/2 "2007-12-05T23:57:19Z")

</div>

On Thu, 6 Dec 2007, Peter B. wrote:

> Once again, a problem with my Befunge interpreter. I’m trying to  
> implement the ~ and & functions - get a character and a number,  
> respectively. My & function looks like this:  
> instructions["&"] = lambda { print "Number: "; stack.push  
> gets().strip.to\_i}
> 
> But it complains about gets() being nil. as soon as the function is run.  
> Well, of course it is. It has let me type. Is there anyway to make it  
> halt?

This works for me:

i= {}  
s= []  
i["&"] = lambda {  
print "Number: "  
s.push( gets().strip.to\_i ) }

i[’&’].call  
puts “Evaluating stack: #{s.pop}”

Gives:

-\> Number:  
123  
-\> Evaluating stack: 123

\*t

–

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [December 6, 2007, 4:28am UTC](https://www.ruby-forum.com/t/lambda-funcs-that-call-gets-wont-halt-program-execution/122256/3 "2007-12-06T04:28:38Z")

</div>

On Dec 5, 2007, at 5:52 PM, Tomas P.'s Mailing L. wrote:

> > Well, of course it is. It has let me type. Is there anyway to make it  
> > i[’&’].call  
> > puts “Evaluating stack: #{s.pop}”
> 
> Gives:
> 
> -\> Number:  
> 123  
> -\> Evaluating stack: 123

And this very similar code works just fine for me.

`
#! /usr/bin/env ruby -w
$stack = []
$instructions = {}
instructions = $instructions
stack = $stack
require "xtest"
instructions["&"].call
p stack
``
instructions = $instructions
stack = $stack
instructions["+"] = lambda { |a, b| stack.push(a + b) }
instructions["-"] = lambda { |a, b| stack.push(b - a) }
instructions["&"] = lambda { print("Number: "); stack.push
(gets.chomp.to_i) }
`

So I wonder: what in your code that you are _not_ showing us is  
messing you up?

Regards, Morton

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [December 6, 2007, 10:22am UTC](https://www.ruby-forum.com/t/lambda-funcs-that-call-gets-wont-halt-program-execution/122256/4 "2007-12-06T10:22:07Z")

</div>

On Dec 5, 4:30 pm, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Once again, a problem with my Befunge interpreter. I’m trying to  
> implement the ~ and & functions - get a character and a number,  
> respectively. My & function looks like this:  
> instructions[“&”] = lambda { print "Number: "; stack.push  
> gets().strip.to\_i}
> 
> But it complains about gets() being nil. as soon as the function is run.  
> Well, of course it is. It has let me type. Is there anyway to make it  
> halt?

If Ruby is invoked in this fashion  
ruby myprog.rb myfile  
then gets reads from the file.  
You need  
$stdin.gets

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [December 6, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/lambda-funcs-that-call-gets-wont-halt-program-execution/122256/5 "2007-12-06T21:57:38Z")

</div>

> You need $stdin.gets

William, you’re a brilliant person. You win. You’re wintastic, in fact.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [December 6, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/lambda-funcs-that-call-gets-wont-halt-program-execution/122256/6 "2007-12-06T22:11:34Z")

</div>

On Dec 6, 2:57 pm, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > You need $stdin.gets
> 
> ## William, you’re a brilliant person. You win. You’re wintastic, in fact.
> 
> Posted viahttp://www.ruby-forum.com/.

I’ve scratched my head because of this behavior, too.  
It’s amazing how something so obvious can cause so  
much puzzlement.
