Class: Binding

Inherits:
Object show all
Defined in:
lib/rmtools/dev/binding.rb

Instance Method Summary collapse

Instance Method Details

#inspect_class_variablesObject



22
23
24
25
26
27
28
29
# File 'lib/rmtools/dev/binding.rb', line 22

def inspect_class_variables
  vars = self.eval('(self.is_a?(Module) ? self : self.class).class_variables') # ['@@a', '@@b']
  if vars and vars.any?
    values = self.eval "{#{vars.map {|v| "'#{v}'=>defined?(#{v})&&#{v}"} * ','}}" # ["@@a's value", "@@b's value"]
    #Hash[vars.zip(values)]
  else {}
  end
end

#inspect_envObject



35
36
37
# File 'lib/rmtools/dev/binding.rb', line 35

def inspect_env
  inspect_local_variables + inspect_instance_variables + inspect_class_variables + inspect_special_variables
end

#inspect_instance_variablesObject



13
14
15
16
17
18
19
20
# File 'lib/rmtools/dev/binding.rb', line 13

def inspect_instance_variables
  vars = self.eval('instance_variables') # ['@a', '@b']
  if vars and vars.any?
    values = self.eval "[#{vars * ','}]" # ["@a's value", "@b's value"]
    Hash[vars.zip(values)]
  else {}
  end
end

#inspect_local_variablesObject



7
8
9
10
11
# File 'lib/rmtools/dev/binding.rb', line 7

def inspect_local_variables
  vars = self.eval('local_variables') # ['a', 'b']
  values = self.eval "[#{vars * ','}]" # ["a's value", "b's value"]
  Hash[vars.zip(values)]
end

#inspect_special_variablesObject



31
32
33
# File 'lib/rmtools/dev/binding.rb', line 31

def inspect_special_variables
  Hash[self.eval(%[{"self" => self, #{['!', '`', '\'', '&', '~', *(1..9)].map {|lit| %{"$#{lit}" => $#{lit}, }}.join}}]).reject {|k,v| v.nil?}]
end

#report(obj) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rmtools/dev/binding.rb', line 43

def report(obj)
  if Array === obj
    obj.map {|s| self.eval "\"#{s.gsub('"'){'\"'}} = \#{(#{s}).inspect}\""} * '; '
  else
    obj.to_s.split(' ').map {|s| self.eval "\"#{s.gsub('"'){'\"'}} = \#{(#{s}).inspect}\""} * '; '
  end
end

#start_interaction(sandbox = true) ⇒ Object

it’s supposed to be called during TDD in an IRB session $__MAIN__ must be ‘self’ or root IRB session, i.e. ‘main’ object

def tested_function
  blah blah blah
rescue => err
  binding.start_interaction
  raise err
end


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rmtools/dev/binding.rb', line 59

def start_interaction(sandbox=true)
  $__env__ = inspect_env
  puts "Caller trace:"
  Kernel.puts RMTools.format_trace(caller(2)).join("\n")
  puts "Environment:"
  $__env__.present
  $__binding__ = self
  if defined? SCRIPT_LINES__ and (file = caller(0)[0].parse(:caller).file) =~ /^\(irb/
    SCRIPT_LINES__["(#{file[1..-2].next_version '#'})"] = []
  end
  
  $__MAIN__.irb$__binding__
  
  if sandbox
    self.eval($__env__.keys.map {|k, v| "#{k} = $__env__[#{k.inspect}]" if k != 'self'} * '; ')
  end
  $__env__ = nil
end

#valid_types(pattern_ary) ⇒ Object



39
40
41
# File 'lib/rmtools/dev/binding.rb', line 39

def valid_types(pattern_ary)
  self.eval("[#{self.eval('local_variables')*','}]").valid_types(pattern_ary)
end