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

P

Peter Bunyan

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. :)

Attachments:
http://www.ruby-forum.com/attachment/1108/befunge.rb
 
T

Tomas Pospisek's Mailing Lists

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
 
M

Morton Goldberg

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

And this very similar code works just fine for me.

<code test.rb>
#! /usr/bin/env ruby -w
$stack = []
$instructions = {}
instructions = $instructions
stack = $stack
require "xtest"
instructions["&"].call
p stack
</code>

<code xtest.rb>
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) }
</code>

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

Regards, Morton
 
W

William James

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
 
P

Peter Bunyan

You need $stdin.gets

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

William James

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

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

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top