Class: Object

Inherits:
BasicObject
Defined in:
lib/blower/util.rb

Instance Method Summary collapse

Instance Method Details

#let(bindings) ⇒ Object

Yield with temporary instance variable assignments.

Examples:

Time

@foo = 1
let :@foo => 2 do
  puts @foo        #=> outputs 2
end
puts @foo          #=> outputs 1

Parameters:

  • bindings (Hash)

    A hash of instance variable names to their temporary values.

Returns:

  • Whatever yielding returns



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/blower/util.rb', line 13

def let (bindings)
  old_values = bindings.keys.map do |key|
    instance_variable_get(key)
  end
  bindings.each do |key, value|
    instance_variable_set(key, value)
  end
  yield
ensure
  return unless old_values
  bindings.keys.each.with_index do |key, i|
    instance_variable_set(key, old_values[i])
  end
end