class RemoteRuby::LocalsExtractor

Extracts local variable from given context

Attributes

block[R]
ignore_types[R]

Public Class Methods

new(block, ignore_types: []) click to toggle source
# File lib/remote_ruby/locals_extractor.rb, line 6
def initialize(block, ignore_types: [])
  @block = block
  @ignore_types = Array(ignore_types)
end

Public Instance Methods

locals() click to toggle source
# File lib/remote_ruby/locals_extractor.rb, line 11
def locals
  locals = {}

  local_variable_names.each do |name|
    value = block.binding.eval(name.to_s)
    next if ignored_type?(value)

    locals[name] = value
  end

  locals
end

Private Instance Methods

ignored_type?(var) click to toggle source
# File lib/remote_ruby/locals_extractor.rb, line 38
def ignored_type?(var)
  ignore_types.any? { |klass| var.is_a? klass }
end
local_variable_names() click to toggle source
# File lib/remote_ruby/locals_extractor.rb, line 26
def local_variable_names
  if RUBY_VERSION >= '2.2'
    block.binding.local_variables
  else
    # A hack to support Ruby 2.1 due to the absence
    # of Binding#local_variables method. For some reason
    # just calling `block.binding.send(:local_variables)`
    # returns variables of the current context.
    block.binding.eval('binding.send(:local_variables)')
  end
end