Class: PrettyIRB::VariableInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_irb/variable_inspector.rb

Instance Method Summary collapse

Constructor Details

#initialize(binding) ⇒ VariableInspector

Returns a new instance of VariableInspector.



3
4
5
# File 'lib/pretty_irb/variable_inspector.rb', line 3

def initialize(binding)
  @binding = binding
end

Instance Method Details

#by_type(type) ⇒ Object

List variables of a specific type



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pretty_irb/variable_inspector.rb', line 40

def by_type(type)
  variables = @binding.local_variables
  filtered = variables.select do |var|
    value = @binding.local_variable_get(var)
    value.class.name.include?(type.capitalize)
  end

  return "❌ No variables of type #{type}" if filtered.empty?

  output = "📦 Variables of type #{type} (#{filtered.length}):\n\n"
  filtered.each do |var|
    value = @binding.local_variable_get(var)
    output += format_variable(var, value)
  end
  output
end

#get(var_name) ⇒ Object

Get variable value



31
32
33
34
35
36
37
# File 'lib/pretty_irb/variable_inspector.rb', line 31

def get(var_name)
  begin
    @binding.local_variable_get(var_name.to_sym)
  rescue NameError
    nil
  end
end

#inspect_var(var_name) ⇒ Object

Get variable details



21
22
23
24
25
26
27
28
# File 'lib/pretty_irb/variable_inspector.rb', line 21

def inspect_var(var_name)
  begin
    value = @binding.local_variable_get(var_name.to_sym)
    detailed_info(var_name, value)
  rescue NameError
    "❌ Variable '#{var_name}' not found"
  end
end

#list_variablesObject

List all local variables



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pretty_irb/variable_inspector.rb', line 8

def list_variables
  variables = @binding.local_variables
  return "📦 No variables defined yet" if variables.empty?

  output = "📦 Local Variables (#{variables.length}):\n\n"
  variables.each do |var|
    value = @binding.local_variable_get(var)
    output += format_variable(var, value)
  end
  output
end

#memory_usageObject

Memory usage estimation



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pretty_irb/variable_inspector.rb', line 73

def memory_usage
  variables = @binding.local_variables
  total_size = variables.sum do |var|
    value = @binding.local_variable_get(var)
    estimate_size(value)
  end

  output = "💾 Memory Usage:\n\n"
  output += "Total: ~#{total_size / 1024} KB\n\n"
  
  variables.each do |var|
    value = @binding.local_variable_get(var)
    size = estimate_size(value)
    output += "  #{var}: ~#{size} bytes\n"
  end
  output
end

#search(keyword) ⇒ Object

Search variables by name



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pretty_irb/variable_inspector.rb', line 58

def search(keyword)
  variables = @binding.local_variables
  filtered = variables.select { |var| var.to_s.include?(keyword) }

  return "🔍 No variables matching '#{keyword}'" if filtered.empty?

  output = "🔍 Found #{filtered.length} variable(s):\n\n"
  filtered.each do |var|
    value = @binding.local_variable_get(var)
    output += format_variable(var, value)
  end
  output
end