public Method

Breakpoint.assert(context = nil) { || ... }

This asserts that the block evaluates to true. If it doesn’t evaluate to true a breakpoint will automatically be created at that execution point.

You can disable assert checking in production code by setting Breakpoint.optimize_asserts to true. (It will still be enabled when Ruby is run via the -d argument.)

Example:

person_name = "Foobar"
assert { not person_name.nil? }

Note: If you want to use this method from an unit test, you will have to call it by its full name, Breakpoint.assert.

Source Code

# File breakpoint.rb, line 270
def assert(context = nil, &condition)
  return if Breakpoint.optimize_asserts and not $DEBUG
  return if yield

  callstack = caller
  callstack.slice!(0, 3) if callstack.first["assert"]
  file, line, method = *callstack.first.match(/^(.+?):(\d+)(?::in `(.*?)')?/).captures

  message = "Assert failed at #{file}:#{line}#{" in `#{method}'" if method}."

  if Breakpoint.asserts_cause_exceptions and not $DEBUG then
    raise(Breakpoint::FailedAssertError, message)
  end

  message += " Executing implicit breakpoint."

  if context then
    return handle_breakpoint(context, message, file, line)
  end

  Binding.of_caller do |context|
    handle_breakpoint(context, message, file, line)
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.