Module: Byebug::VarFunctions

Included in:
VarCommand
Defined in:
lib/byebug/commands/var.rb

Overview

Utilities for the var command.

Instance Method Summary collapse

Instance Method Details

#var_all(_str = nil) ⇒ Object



53
54
55
56
57
# File 'lib/byebug/commands/var.rb', line 53

def var_all(_str = nil)
  var_global
  var_instance('self')
  var_local
end

#var_constant(str) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/byebug/commands/var.rb', line 37

def var_constant(str)
  str ||= 'self.class'
  obj = bb_warning_eval(str)
  is_mod = obj.is_a?(Module)
  return errmsg(pr('variable.errors.not_module', object: str)) unless is_mod

  constants = bb_eval("#{str}.constants")
  puts prv(constants.sort.map { |c| [c, obj.const_get(c)] })
end

#var_global(_str = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/byebug/commands/var.rb', line 24

def var_global(_str = nil)
  globals = global_variables.reject do |v|
    [:$IGNORECASE, :$=, :$KCODE, :$-K, :$binding].include?(v)
  end

  var_list(globals)
end

#var_instance(str) ⇒ Object



32
33
34
35
# File 'lib/byebug/commands/var.rb', line 32

def var_instance(str)
  obj = bb_warning_eval(str || 'self')
  var_list(obj.instance_variables, obj.instance_eval { binding })
end

#var_list(ary, b = get_binding) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/byebug/commands/var.rb', line 8

def var_list(ary, b = get_binding)
  vars = ary.sort.map do |v|
    s = begin
      b.eval(v.to_s).inspect
    rescue
      begin
        b.eval(v.to_s).to_s
      rescue
        '*Error in evaluation*'
      end
    end
    [v, s]
  end
  puts prv(vars)
end

#var_local(_str = nil) ⇒ Object



47
48
49
50
51
# File 'lib/byebug/commands/var.rb', line 47

def var_local(_str = nil)
  _self = @state.context.frame_self(@state.frame)
  locals = @state.context.frame_locals
  puts prv(locals.keys.sort.map { |k| [k, locals[k]] })
end