Class: Rink::Namespace

Inherits:
Object show all
Defined in:
lib/rink/namespace.rb

Instance Method Summary collapse

Constructor Details

#initialize(ns = self.default_ns) ⇒ Namespace

Returns a new instance of Namespace.



3
4
5
# File 'lib/rink/namespace.rb', line 3

def initialize(ns = self.default_ns)
  replace(ns)
end

Instance Method Details

#bindingObject



7
8
9
# File 'lib/rink/namespace.rb', line 7

def binding
  @ns_binding
end

#default_nsObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/rink/namespace.rb', line 26

def default_ns
  # We want namespace to be any object, and in order to do that, Rink will call namespace#binding.
  # But by default, the namespace should be TOPLEVEL_BINDING. If we set @namespace to this,
  # Rink will call TOPLEVEL_BINDING#binding, which is an error. So instead we'll create a singleton
  # object and override #binding on that object to return TOPLEVEL_BINDING. Effectively, that
  # singleton object becomes (more-or-less) a proxy into the toplevel object. (Is there a better way?)
  klass = Class.new(Object)
  klass.send(:define_method, :binding) { TOPLEVEL_BINDING }
  klass.new
end

#evaluate(code, *filename_lineno) ⇒ Object



22
23
24
# File 'lib/rink/namespace.rb', line 22

def evaluate(code, *filename_lineno)
  eval code, @ns_binding, *filename_lineno
end

#nsObject



11
12
13
# File 'lib/rink/namespace.rb', line 11

def ns
  @namespace
end

#replace(ns) ⇒ Object



15
16
17
18
19
20
# File 'lib/rink/namespace.rb', line 15

def replace(ns)
  return ns if ns.eql?(@namespace)
  @ns_binding = ns.instance_eval { binding }
          #ns.send(:binding) # this no worky in 1.9
  @namespace = ns
end