------art_3155_26309858.1228366833588
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

> ## Befunge (#184)

I haven't actually finished a Ruby Quiz since #13. Four years is a
long time! But this one I had to do. Literally the day before the quiz
was posted I was talking with some coworkers about Befunge, Brainf*ck,
and the like. That this was the very next Ruby Quiz was fate.

Because I was going slowly, I peeked at some of the answers here, I
did borrow a little. I love to use the Hash of Procs approach to the
the command pattern, which someone here talked about. Matthew's trick
using % to do the wrap-arounds was really cool, so I had to use that.

Most importantly, even before I saw all the talk about writing a
debugger, I had decided the real challenge was to make a GUI IDE for
Befunge. After looking at some options, I decided to implement it in
Rails. So that gave me a few days in which to learn Rails (I've been
procrastinating a few years on that). I also had to move my personal
web site to a Rails-friendly host (and way more Ruby/programmer
friendly, in general). So this quiz really helped me get going on my
"professional development" to-do list. :)

I didn't quite get all the way to an IDE, but it is fun to watch a
Befunge program execute. With fancy 2D graphics and 8-bit color and
everything.

I also didn't conquer the input problem, so I just have stub methods
that use random numbers for that.

So here my befunge.rb -- which acts as interpreter, debugger, and
Rails model class (you'd drop it in app/models)

If you want to see the Rails-based Befunge "IDE" in action, head over
to http://www.mikelibby.com/befunge

If anyone wants to see the relevant Rails bits, just ask and I'll put
it on GitHub.

-Michael C. Libby
www.mikelibby.com

------art_3155_26309858.1228366833588
Content-Type: application/x-ruby; name=befunge.rb
Content-Transfer-Encoding: base64
X-Attachment-Id: f_foax4tvi0
Content-Disposition: attachment; filename=befunge.rb

Y2xhc3MgQmVmdW5nZQoKICBhdHRyX2FjY2Vzc29yIDpkaXJlY3Rpb24sIDpub19za2lwLCA6c3Ry
aW5nbW9kZQogIGF0dHJfcmVhZGVyIDpoZWlnaHQsIDp3aWR0aCwgOm91dHB1dCwgOnBjX3gsIDpw
Y195LCA6cHJvZ3JhbSwgOnN0YWNrCgogICMgY29tbWFuZHMgYXJlIGRvbmUgdGhpcyB3YXkgdG8g
bWFrZSBpdCBlYXN5IHRvIHB1c2ggYSB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gY2hh
cnMuZWFjaCBkbyB8Y2hhcnwKICAgICAgICBAcHJvZ3JhbVtyb3dfbnVtYmVyXVtjb2x1bW5fbnVt
YmVyXSA9IGNoYXIKICAgICAgICBjb2x1bW5fbnVtYmVyICs9IDEKICAgICAgZW5kCiAgICAgIHJv
d19udW1iZXIgKz0gMQogICAgZW5kCiAgZW5kCgogIGRlZiBwb3AKICAgIHggPSBAc3RhY2sucG9w
CiAgICB4Lm5pbD8gPyAwIDogeAogIGVuZAoKICBkZWYgcG9waQogICAgeCA9IHBvcAogICAgaSA9
ICh4ID09IHgudG9faSkgPyB4IDogeFswXQogIGVuZAoKICBkZWYgcG9wMgogICAgW3BvcCwgcG9w
XQogIGVuZAoKICBkZWYgcG9wMmkKICAgIFtwb3BpLCBwb3BpXQogIGVuZAoKICBkZWYgcHVzaChh
KQogICAgQHN0YWNrIDw8IGEKICBlbmQKCiAgZGVmIHByaW50KHMpCiAgICBpZiBAcHJpbnQubmls
PwogICAgICBAb3V0cHV0IDw8IHMKICAgIGVsc2UKICAgICAgQHByaW50LmNhbGwocykKICAgIGVu
ZAogIGVuZAoKICBkZWYgcHJpbnRfYShjKQogICAgaWYgYy5yZXNwb25kX3RvPyg6Y2hyKQogICAg
ICBwcmludChjLmNocikKICAgIGVsc2UKICAgICAgcHJpbnQoYykKICAgIGVuZAogIGVuZAoKICBk
ZWYgcHJpbnRfaShjKQogICAgaWYgYy5yZXNwb25kX3RvPyg6Y2hyKQogICAgICBwcmludChjLnRv
X3MpCiAgICBlbHNlCiAgICAgIHByaW50KGNbMF0udG9fcykKICAgIGVuZAogIGVuZAoKICBkZWYg
c2V0X2NlbGwodmFsdWUsIHgsIHkpCiAgICBpZiB2YWx1ZS5yZXNwb25kX3RvPyg6Y2hyKQogICAg
ICBAcHJvZ3JhbVt5LnRvX2ldW3gudG9faV0gPSB2YWx1ZS5jaHIKICAgIGVsc2UKICAgICAgQHBy
b2dyYW1beS50b19pXVt4LnRvX2ldID0gdmFsdWUKICAg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