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:
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? I could do something like:
File.open(“befunge-93.rb”).each { |instruction| eval(instruction }
But it that’s a valid solution, then I’m Tony Blair.
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:
Ah, thanks. With Ruby I’m used to not having to do global/local things;
I didn’t know you could create global ones with $. But now I have
another problem: The functions I created with lambda {} are now
returning -1 as their arity.
Ah, thanks. With Ruby I’m used to not having to do global/local things;
I didn’t know you could create global ones with $. But now I have
another problem: The functions I created with lambda {} are now
returning -1 as their arity.
AFAIK, that’s a quirk in 1.8. Blocks with no arguments return -1 when
you
ask them for their arity. That behavior was redefined in 1.9, and they
now
return 0. You can also, currently, pass arguments to those blocks
without
any indication that they’ll be ignored.