Class: Google::Cloud::Debugger::Breakpoint::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/debugger/breakpoint/variable.rb

Overview

Variable

Represents a variable or an argument possibly of a compound object type. Note how the following variables are represented:

A simple Variable:

  x = 5
  # Captured variable:
  # { name: "x", value: "5", type: "Integer" }

A Compound Variable:

  class T
    attr_accessor :m1, :m2
    ...
  end
  v = T.new(1, "2")
  # Captured variable:
  # {
  #   name: "v",
  #   type: "T",
  #   members: [
  #     { name: "@m1", value: "1", type: "Integer" },
  #     { name: "@m2", value: "2", type: "String" }
  #   ]
  # }

A Hash object:

  hash = { a: 1, b: :two }
  # Captured variable:
  # {
  #   name: "hash",
  #   type: "Hash",
  #   members: [
  #     { name: "a", value: "1", type: "Integer" },
  #     { name: "b", value: ":2", type: "Symbol" }
  #   ]
  # }

An Array object:

  ary = [1, nil]
  # Captured variable:
  # {
  #   name: "ary",
  #   type: "Array",
  #   members: [
  #     { name: "[0]", value: "1", type: "Integer" },
  #     { name: "[1]", value: "nil", type: "NilClass" }
  #   ]
  # }

Constant Summary collapse

MAX_DEPTH =

Max depth to convert on compound variables

3
MAX_MEMBERS =

Max number of member variables to evaluate in compound variables

10
MAX_STRING_LENGTH =

Max length on variable inspect results. Truncate extra and replace with ellipsis.

260

Class Method Summary collapse

Class Method Details

.from_rb_var(source, name: nil, depth: MAX_DEPTH) ⇒ Google::Cloud::Debugger::Breakpoint::Variable

Convert a Ruby variable into a Google::Cloud::Debugger::Breakpoint::Variable object.

Examples:

x = 3
var = Variable.from_rb_var x, name: "x"
var.name  #=> "x"
var.value #=> "3"
var.type  #=> "Integer"
hash = {a: 1, b: :two}
var = Variable.from_rb_var hash, name: "hash"
var.name  #=> "hash"
var.type  #=> "Hash"
var.members[0].name  #=> "a"
var.members[0].value #=> "1"
var.members[0].type  #=> "Integer"
var.members[1].name  #=> "b"
var.members[1].value #=> "two"
var.members[1].type  #=> "Symbol"
foo = Foo.new(a: 1, b: []) #=> #<Foo:0x0000 @a: 1, @b: []>
var = Variable.from_rb_var foo, name: "foo"
var.name  #=> "foo"
var.type  #=> "Foo"
var.members[0].name  #=> "@a"
var.members[0].value #=> "1"
var.members[0].type  #=> "Integer"
var.members[1].name  #=> "@b"
var.members[1].value #=> "[]"
var.members[1].type  #=> "Array"

Parameters:

  • source (Any)

    Source Ruby variable to convert from

  • name (String)

    Name of the varaible

  • depth (Integer)

    Number of levels to evaluate in compound variables. Default to MAX_DEPTH

Returns:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/google/cloud/debugger/breakpoint/variable.rb', line 177

def self.from_rb_var source, name: nil, depth: MAX_DEPTH
  return source if source.is_a? Variable

  # If source is a non-empty Array or Hash, or source has instance
  # variables, evaluate source as a compound variable.
  if (((source.is_a?(Hash) || source.is_a?(Array)) &&
     !source.empty?) || !source.instance_variables.empty?) &&
     depth > 0
    from_compound_var source, name: name, depth: depth
  else
    var = Variable.new
    var.name = name.to_s if name
    var.type = source.class.to_s
    var.value = truncate_value(source.inspect)

    var
  end
end