module Byebug::Helpers::EvalHelper

Utilities to assist evaluation of code strings

Public Instance Methods

error_eval(str, binding = frame._binding) click to toggle source

Evaluates a string containing Ruby code in a specific binding, handling the errors at an error level.

# File lib/byebug/helpers/eval.rb, line 37
def error_eval(str, binding = frame._binding)
  safe_eval(str, binding) { |e| fail(e, msg(e)) }
end
silent_eval(str, binding = frame._binding) click to toggle source

Evaluates a string containing Ruby code in a specific binding, returning nil in an error happens.

# File lib/byebug/helpers/eval.rb, line 29
def silent_eval(str, binding = frame._binding)
  safe_eval(str, binding) { |_e| nil }
end
single_thread_eval(expression) click to toggle source

Evaluates an expression that doesn't deal with threads

@param expression [String] Expression to evaluate

# File lib/byebug/helpers/eval.rb, line 21
def single_thread_eval(expression)
  warning_eval(expression)
end
thread_safe_eval(expression) click to toggle source

Evaluates expression that might manipulate threads

@param expression [String] Expression to evaluate

# File lib/byebug/helpers/eval.rb, line 12
def thread_safe_eval(expression)
  allowing_other_threads { single_thread_eval(expression) }
end
warning_eval(str, binding = frame._binding) click to toggle source

Evaluates a string containing Ruby code in a specific binding, handling the errors at a warning level.

# File lib/byebug/helpers/eval.rb, line 45
def warning_eval(str, binding = frame._binding)
  safe_eval(str, binding) { |e| errmsg(msg(e)) }
end

Private Instance Methods

allowing_other_threads() { || ... } click to toggle source

Run block temporarily ignoring all TracePoint events.

Used to evaluate stuff within Byebug's prompt. Otherwise, any code creating new threads won't be properly evaluated because new threads will get blocked by byebug's main thread.

# File lib/byebug/helpers/eval.rb, line 82
def allowing_other_threads
  Byebug.unlock
  res = yield
  Byebug.lock
  res
end
error_msg(e) click to toggle source
# File lib/byebug/helpers/eval.rb, line 63
def error_msg(e)
  at = e.backtrace

  locations = ["#{at.shift}: #{warning_msg(e)}"]
  locations += at.map { |path| "\tfrom #{path}" }
  locations.join("\n")
end
msg(e) click to toggle source
# File lib/byebug/helpers/eval.rb, line 57
def msg(e)
  msg = Setting[:stack_on_error] ? error_msg(e) : warning_msg(e)

  pr('eval.exception', text_message: msg)
end
safe_eval(str, binding) { |e| ... } click to toggle source
# File lib/byebug/helpers/eval.rb, line 51
def safe_eval(str, binding)
  binding.eval(str)
rescue StandardError, ScriptError => e
  yield(e)
end
warning_msg(e) click to toggle source
# File lib/byebug/helpers/eval.rb, line 71
def warning_msg(e)
  "#{e.class} Exception: #{e.message}"
end