summaryrefslogtreecommitdiff
path: root/lib/pry/testable/variables.rb
blob: 0de7d3bc288af3c26c4e196fbf9a6e7ec6a094e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Pry::Testable::Variables
  #
  # @example
  #   temporary_constants(:Foo, :Bar) do
  #     Foo = Class.new(RuntimeError)
  #     Bar = Class.new(RuntimeError)
  #   end
  #   Foo # => NameError
  #   Bar # => NameError
  #
  # @param [Array<Symbol>] names
  #   An array of constant names that be defined by a block,
  #   and removed by this method afterwards.
  #
  # @return [void]
  #
  def temporary_constants(*names)
    names.each do |name|
      Object.remove_const name if Object.const_defined?(name)
    end
    yield
  ensure
    names.each do |name|
      Object.remove_const name if Object.const_defined?(name)
    end
  end

  #
  # @param [String] name
  #   The name of a variable.
  #
  # @param [String] value
  #   Its value.
  #
  # @param [Binding] b
  #   The binding object to insert a variable into.
  #
  # @return [void]
  #
  def insert_variable(name, value, b)
    Pry.current[:pry_local] = value
    b.eval("#{name} = ::Pry.current[:pry_local]")
  ensure
    Pry.current[:pry_local] = nil   
  end
end