Class: Rubic::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rubic/environment.rb

Instance Method Summary collapse

Constructor Details

#initialize(outer = nil) ⇒ Environment



3
4
5
6
# File 'lib/rubic/environment.rb', line 3

def initialize(outer=nil)
  @outer = outer
  @table = {}
end

Instance Method Details

#bind(params, args) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/rubic/environment.rb', line 22

def bind(params, args)
  if params.size != args.size
    raise Rubic::ArgumentError, "wrong number of arguments (#{args.size} for #{params.size})"
  end

  params.each.with_index do |name, i|
    @table[name] = args[i]
  end
end

#defvar(name, value) ⇒ Object Also known as: []=



8
9
10
# File 'lib/rubic/environment.rb', line 8

def defvar(name, value)
  @table[name] = value
end

#extend(klass) ⇒ Object



32
33
34
35
36
37
# File 'lib/rubic/environment.rb', line 32

def extend(klass)
  ext = klass.new
  klass.instance_methods(false).each do |mname|
    defvar(mname, ext.method(mname))
  end
end

#refvar(name) ⇒ Object Also known as: []



12
13
14
15
16
17
18
19
20
# File 'lib/rubic/environment.rb', line 12

def refvar(name)
  if @table.key? name
    @table[name]
  elsif @outer
    @outer.refvar(name)
  else
    raise Rubic::NameError, "undefined variable `#{name}'"
  end
end