Module: Kernel

Defined in:
lib/epubforge/core_extensions/kernel.rb

Instance Method Summary collapse

Instance Method Details

#silence_warnings(&block) ⇒ Object

Runs a block of code without warnings.



23
24
25
26
27
28
29
# File 'lib/epubforge/core_extensions/kernel.rb', line 23

def silence_warnings(&block)
  warn_level = $VERBOSE
  $VERBOSE = nil
  result = block.call
  $VERBOSE = warn_level
  result
end

#with_locals(locals = {}, &block) ⇒ Object

yields an alternate reality block where instance methods are different from what they were. At the end of the block it resets the initial values of the instance variables.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/epubforge/core_extensions/kernel.rb', line 5

def with_locals locals = {}, &block
  old_local_vars = {}

  for k, v in locals
    var = :"@#{k}"
    old_local_vars[k] = instance_variable_get(var)
    instance_variable_set( var, v )
  end

  yield
ensure      # make all as it once was
  for k, v in old_local_vars
    var = :"@#{k}"
    instance_variable_set( var, v )
  end
end