From: MonkeeSage Date: 2007-12-06T04:50:15+09:00 Subject: Re: Keeping variables across requires? On Dec 5, 1:13 pm, Peter Bunyan wrote: > 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. > > My new code: > > befunge.rb > --- > elsif $instructions.include? operator > arguments = [] > $instructions[operator].arity.times {arguments.push stack.pop} > $instructions[operator].call( *arguments ) > else > --- > > befunge-93.rb (just a sample) > --- > $instructions = {} > $instructions["!"] = lambda { |a| stack.push !a} > $instructions["`"] = lambda { |a, b| stack.push b>a ? 1 : 0 } > $instructions[">"] = lambda { xvel = 1; yvel = 0 } > --- > -- > Posted viahttp://www.ruby-forum.com/. Global state is usually unnecessary. It looks like your code would fit naturally into a layout similar to this... ==befunge-93.rb== require 'singleton' class Befunge93 include Singleton attr_accessor :instructions, :stack def initialize @stack = [] @instructions = {} @instructions["!"] = lambda { | a | @stack.push(!a) } @instructions["`"] = lambda { | a, b | @stack.push(b>a ? 1 : 0) } @instructions[">"] = lambda { xvel = 1 yvel = 0 } end end ==== ==befunge.rb== require 'befunge-93' b = Befunge93.instance #... elsif b.instructions.include?(operator) arguments = [] b.instructions[operator].arity.times { arguments.push(b.stack.pop) } b.instructions[operator].call(*arguments) else #... Regards, Jordan