# google-cloud-debugger - Class Google::Cloud::Debugger::Breakpoint::Variable (v0.42.2)

Reference documentation and code samples for the google-cloud-debugger class Google::Cloud::Debugger::Breakpoint::Variable.

## Variable

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

A simple Variable:
`ruby
x = 5
# Captured variable:
# { name: "x", value: "5", type: "Integer" }
`

A Compound Variable:
`ruby
class T
attr_accessor :m1, :m2
...
end
v = T.new(1, "2")
# Captured variable:
# {
# name: "v",
# type: "T",
# members: [
# { name: &quot;@m1&quot;, value: "1", type: "Integer" },
# { name: &quot;@m2&quot;, value: "2", type: "String" }
# ]
# }
`

A Hash object:
`ruby
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:
`ruby
ary = [1, nil]
# Captured variable:
# {
# name: "ary",
# type: "Array",
# members: [
# { name: "[0]", value: "1", type: "Integer" },
# { name: "[1]", value: "nil", type: "NilClass" }
# ]
# }
`

## Inherits

- Object

## Methods

### .from_rb_var

    def self.from_rb_var(source, name: nil, depth: MAX_DEPTH, var_table: nil, limit: nil) -> Google::Cloud::Debugger::Breakpoint::Variable

Convert a Ruby variable into a
Google::Cloud::Debugger::Breakpoint::Variable object. If
a variable table is provided, it will store all the subsequently
created compound variables into the variable table for sharing.
**Parameters**

- **source** (Any) --- Source Ruby variable to convert from
- **name** (String) *(defaults to: nil)* --- Name of the varaible
- **depth** (Integer) *(defaults to: MAX_DEPTH)* --- Number of levels to evaluate in compound variables. Default to [MAX_DEPTH](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-debugger/latest/Google-Cloud-Debugger-Breakpoint-Variable#Google__Cloud__Debugger__Breakpoint__Variable__MAX_DEPTH "Google::Cloud::Debugger::Breakpoint::Variable::MAX_DEPTH (constant)")
- **var_table** ([Breakpoint::VariableTable](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-debugger/latest/Google-Cloud-Debugger-Breakpoint-VariableTable)) *(defaults to: nil)* --- A variable table to store shared compound variables. Optional.
- **limit** (Integer) *(defaults to: nil)* --- Maximum number of bytes this conversion should take. This include nested compound member variables' conversions.
**Returns**

- ([Google::Cloud::Debugger::Breakpoint::Variable](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-debugger/latest/Google-Cloud-Debugger-Breakpoint-Variable)) --- Converted variable.
**Examples**

Simple variable conversion

```ruby
x = 3.0
var = Google::Cloud::Debugger::Breakpoint::Variable.from_rb_var \
        x, name: "x"
var.name  #=> "x"
var.value #=> "3.0"
var.type  #=> "Float"
```

Hash conversion

```ruby
hash = {a: 1.0, b: :two}
var = Google::Cloud::Debugger::Breakpoint::Variable.from_rb_var \
        hash, name: "hash"
var.name  #=> "hash"
var.type  #=> "Hash"
var.members[0].name  #=> "a"
var.members[0].value #=> "1.0"
var.members[0].type  #=> "Float"
var.members[1].name  #=> "b"
var.members[1].value #=> ":two"
var.members[1].type  #=> "Symbol"
```

Custom compound variable conversion

```ruby
foo = Foo.new(a: 1.0, b: [])
foo.inspect  #=> "#<Foo:0xXXXXXX @a=1.0, @b=[]>"
var = Google::Cloud::Debugger::Breakpoint::Variable.from_rb_var \
        foo, name: "foo"
var.name  #=> "foo"
var.type  #=> "Foo"
var.members[0].name  #=> "@a"
var.members[0].value #=> "1.0"
var.members[0].type  #=> "Float"
var.members[1].name  #=> "@b"
var.members[1].value #=> "[]"
var.members[1].type  #=> "Array"
```

Use variable table for shared compound variables

```ruby
hash = {a: 1.0}
ary = [hash, hash]
var_table = Google::Cloud::Debugger::Breakpoint::VariableTable.new
var = Google::Cloud::Debugger::Breakpoint::Variable.from_rb_var \
        ary, name: "ary", var_table: var_table
var.name            #=> "ary"
var.var_table_index #=> 0
var_table[0].type   #=> "Array"
var_table[0].members[0].name            #=> "[0]"
var_table[0].members[0].var_table_index #=> 1
var_table[0].members[1].name            #=> "[1]"
var_table[0].members[1].var_table_index #=> 1
var_table[1].type #=> "Hash"
var_table[1].members[0].name #=> "a"
var_table[1].members[0].type #=> "Float"
var_table[1].members[0].value #=> "1.0"
```

### #payload_size

    def payload_size() -> Integer

Calculate the bytesize of all the attributes and that of the
member variables.
**Returns**

- (Integer) --- The total payload size of this variable in bytes.

### #set_error_state

    def set_error_state(message, refers_to: StatusMessage::VARIABLE_VALUE)

Set this variable to an error state by setting the status field

### #to_grpc

    def to_grpc()

Exports the Variable to a
Google::Cloud::Debugger::V2::Variable object.

### #total_size

    def total_size() -> Integer

Calculate the total bytesize of all the attributes and that of the
member variables, plus references into other variables in the
variable table.
**Returns**

- (Integer) --- The total payload size of this variable in bytes.

## Constants

### MAX_DEPTH

**value:** 3  
Max depth to convert on compound variables

### MAX_MEMBERS

**value:** 1000  
Max number of member variables to evaluate in compound variables

### MAX_STRING_LENGTH

**value:** 500  
Max length on variable inspect results. Truncate extra and replace
with ellipsis.