class Gen::Code

Attributes

bound_constants[RW]
bound_procs[RW]
local_var_count[RW]

Public Class Methods

new(local_var_count=0, bound_procs=[], bound_constants=[]) click to toggle source
# File lib/gen/code.rb, line 18
def initialize(local_var_count=0, bound_procs=[], bound_constants=[])
  @local_var_count = local_var_count
  @bound_procs     = bound_procs
  @bound_constants = bound_constants
end

Public Instance Methods

bind_const(a_const) click to toggle source

Bound within `#bound_eval` as `const_#{position in @bound_constants}`

# File lib/gen/code.rb, line 65
def bind_const(a_const)
  const_string = "const_#{@bound_constants.length}"
  @bound_constants << a_const.freeze
  const_string
end
bind_proc(a_proc) click to toggle source

Bound within `#bound_eval` as `proc_#{position in @bound_procs}`

# File lib/gen/code.rb, line 55
def bind_proc(a_proc)
  unless a_proc.is_a? Proc
    raise ArgumentError, "#{a_proc.inspect} is not a Proc, it is a #{a_proc.class}".freeze
  end
  proc_string = "proc_#{@bound_procs.length}"
  @bound_procs     << a_proc.freeze
  proc_string
end
bound_eval(source) click to toggle source

Evaluate a string using the locally bound procs and constants

# File lib/gen/code.rb, line 25
def bound_eval(source)
  self.generate_binding.eval source
end
generate_binding(a_binding=binding) click to toggle source

Generate a binding containing the locally bound procs and constants

# File lib/gen/code.rb, line 30
def generate_binding(a_binding=binding)
  a_binding.local_variables do |local_var|
    a_binding.local_variable_set local_var, nil
  end
  @bound_procs.zip(0..Float::INFINITY).each do |bound_proc, num|
    a_binding.local_variable_set "proc_#{num}",  bound_proc
  end
  @bound_constants.zip(0..Float::INFINITY).each do |bound_const, num|
    a_binding.local_variable_set "const_#{num}", bound_const
  end
  a_binding
end
local_var() click to toggle source

The current local variable, of form: `“x_#{@local_var_count}”`

# File lib/gen/code.rb, line 44
def local_var
  "x_#{@local_var_count}"
end
new_local_var(other) click to toggle source

Assigns a new local variable to its argument (incrementing `@local_var_count`)

# File lib/gen/code.rb, line 49
def new_local_var(other)
  @local_var_count += 1
  "#{self.local_var} = #{other}"
end