Morton Goldberg
12/5/2007 6:59:00 PM
On Dec 5, 2007, at 12:30 PM, Peter Bunyan wrote:
> I'm making a Befunge interpreter in Ruby - I like to keep my brain
> active. I want to be able to switch between different sets of
> rules, so
> as well as my base program befunge.rb, I've got a file called
> befunge-93.rb that contains the Befunge-93 specification. Like so:
>
> befunge.rb
> ---
> instructions = {}
> require 'befunge-93'
> ---
>
> befunge-93.rb
> ---
> instructions = {}
> instructions["+"] = lambda { |a, b| stack.push a+b }
> instructions["-"] = lambda { |a, b| stack.push b-a }
> more instructions, snipped.
> ---
>
> I do the 'instructions = {}' line twice, as it curls up and dies
> without
> it. But after I've included the instructions file, instructions is
> still
> {}. This hints at the fact that it's not letting me use the
> instructions
> variable across the file.
>
> Is there any way to do this?
You really have two variables 'instructions', each local to file in
which it appears. You could use the following trick with a global
variable:
<code test.rb>
#! /usr/bin/env ruby -w
$instructions = {}
instructions = $instructions
require "xtest"
p instructions
</code>
<code xtest.rb>
instructions = $instructions
instructions["+"] = lambda { |a, b| stack.push a+b }
instructions["-"] = lambda { |a, b| stack.push b-a }
</code>
Regards, Morton