Class: Binding

Inherits:
Object
  • Object
show all
Defined in:
lib/sandbox/binding.rb

Instance Method Summary collapse

Instance Method Details

#[](x) ⇒ Object

Returns the value of some variable.

a = 2
binding["a"]  #=> 2


7
8
9
10
11
12
13
# File 'lib/sandbox/binding.rb', line 7

def [](x)
  if RUBY_VERSION.to_f > 2.1
    local_variable_get(x)
  else
    eval(x.to_s)
  end
end

#[]=(l, v) ⇒ Object

Set the value of a local variable.

b = binding
b["a"] = 4
eval("a", b)  #=> 4


20
21
22
23
24
25
26
# File 'lib/sandbox/binding.rb', line 20

def []=(l, v)
  if RUBY_VERSION.to_f > 2.1
    local_variable_set(l, v)
  else
    eval("#{l} = nil; lambda {|v| #{l} = v}").call(v)
  end
end