Ruby: include/ruby/assert.h File Reference

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

Macros
assert.h File Reference
`#include "ruby/internal/assume.h"`
`#include "ruby/internal/attr/cold.h"`
`#include "ruby/internal/attr/format.h"`
`#include "ruby/internal/attr/noreturn.h"`
`#include "ruby/internal/cast.h"`
`#include "ruby/internal/dllexport.h"`
`#include "ruby/backward/2/assume.h"`

Go to the source code of this file.

## Macros
#define | RUBY_DEBUG 0
| Define this macro when you want assertions.
#define | NDEBUG
| Define this macro when you don't want assertions.
#define | RUBY_NDEBUG 1
| This macro is basically the same as NDEBUG.
#define | RBIMPL_VA_OPT_ARGS(...)
| Prints the given message, and terminates the entire process abnormally.
#define | RUBY_ASSERT_FAIL(mesg, ...)
#define | RUBY_ASSERT_MESG(expr, ...)
| Asserts that the expression is truthy.
#define | RUBY_ASSERT_ALWAYS(expr, ...)
| A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define | RUBY_ASSERT(...)
| Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define | RUBY_ASSERT_NDEBUG(...)
| A variant of RUBY_ASSERT that interfaces with NDEBUG instead of RUBY_DEBUG.
#define | RUBY_ASSERT_MESG_WHEN(cond, expr, ...)
| A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy.
#define | RUBY_ASSERT_WHEN(cond, expr, ...)
| A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy.
#define | RUBY_ASSERT_BUILTIN_TYPE(obj, type)
| A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or built-in type of obj is type.
#define | RBIMPL_ASSERT_OR_ASSUME(...)
| This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.

## Detailed Description

Author
  Ruby developers ruby-.nosp@m.core.nosp@m.@ruby.nosp@m.-lan.nosp@m.g.org

Date
  Wed May 18 00:21:44 JST 1994

Copyright
  This file is a part of the programming language Ruby. Permission is hereby granted, to either redistribute and/or modify this file, provided that the conditions mentioned in the file COPYING are met. Consult the file for details.

Warning
  Symbols prefixed with either RBIMPL or rbimpl are implementation details. Don't take them as canon. They could rapidly appear then vanish. The name (path) of this header file is also an implementation detail. Do not expect it to persist at the place it is now. Developers are free to move it anywhere anytime at will.

Note
  To ruby-core: remember that this header can be possibly recursively included from extension libraries written in C++. Do not expect for instance __VA_ARGS__ is always available. We assume C99 for ruby itself but we don't assume languages of extension libraries. They could be written in C++98.

Definition in file assert.h.

## Macro Definition Documentation

## ◆ NDEBUG

#define NDEBUG

Define this macro when you don't want assertions.

Definition at line 90 of file assert.h.

## ◆ RBIMPL_ASSERT_OR_ASSUME

#define RBIMPL_ASSERT_OR_ASSUME | ( |  | ... | ) |

Value:
RBIMPL_ASSERT_NOTHING

This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.

Parameters

  expr | What supposedly evaluates to true.

Definition at line 311 of file assert.h.

Referenced by RARRAY_EMBED_LEN(), RB_BUILTIN_TYPE(), rb_fd_max(), RB_FIX2INT(), RB_FIX2UINT(), rb_fix2ulong(), RB_FL_ABLE(), RB_FL_REVERSE_RAW(), RB_FL_SET_RAW(), RB_FL_TEST_RAW(), RB_FL_UNSET_RAW(), RB_INT2FIX(), RB_OBJ_PROMOTED_RAW(), rb_rand_if(), RB_ST2FIX(), RBASIC_CLASS(), and ruby_scan_digits().

## ◆ RBIMPL_VA_OPT_ARGS

#define RBIMPL_VA_OPT_ARGS | ( |  | ... | ) |

Value:
__VA_OPT__(,) __VA_ARGS__

Prints the given message, and terminates the entire process abnormally.

Parameters

  mesg | The message to display.

Definition at line 162 of file assert.h.

## ◆ RUBY_ASSERT

#define RUBY_ASSERT | ( |  | ... | ) |

Value:
RBIMPL_ASSERT_NOTHING

Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.

Parameters

  expr | What supposedly evaluates to true.

Definition at line 219 of file assert.h.

Referenced by class_alloc0(), rb_absint_numwords(), rb_check_symbol(), rb_class_new(), rb_class_superclass(), rb_copy_generic_ivar(), rb_fiber_scheduler_blocking_operation_cancel(), rb_fiber_scheduler_close(), rb_fiber_scheduler_current(), rb_fiber_scheduler_get(), rb_fiber_scheduler_set(), rb_fiber_scheduler_unblock(), rb_include_module(), rb_ivar_count(), rb_mod_init_copy(), rb_obj_is_kind_of(), rb_prepend_module(), rb_str_hash(), rb_str_modify_expand(), rb_str_set_len(), rb_str_update(), rb_string_value(), rb_sym2id(), and rb_tracepoint_disable().

## ◆ RUBY_ASSERT_ALWAYS

#define RUBY_ASSERT_ALWAYS | ( |  | expr,
|  |  | ... )

Value:
RUBY_ASSERT_MESG(expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
RUBY_ASSERT_MESG
#define RUBY_ASSERT_MESG(expr,...)
Asserts that the expression is truthy.
Definition assert.h:186
RBIMPL_VA_OPT_ARGS
#define RBIMPL_VA_OPT_ARGS(...)
Prints the given message, and terminates the entire process abnormally.
Definition assert.h:162

A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.

Parameters

  expr | What supposedly evaluates to true.

Definition at line 199 of file assert.h.

Referenced by rb_data_object_wrap(), rb_memerror(), and rb_thread_call_with_gvl().

## ◆ RUBY_ASSERT_BUILTIN_TYPE

#define RUBY_ASSERT_BUILTIN_TYPE | ( |  | obj,
|  |  | type )

Value:
RUBY_ASSERT(RB_TYPE_P(obj, type), \
"Actual type is %s", rb_builtin_type_name(BUILTIN_TYPE(obj)))
RUBY_ASSERT
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
BUILTIN_TYPE
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376

A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or built-in type of obj is type.

Parameters

  obj | Object to check its built-in typue.
  type | Built-in type constant, T_ARRAY, T_STRING, etc.

Definition at line 291 of file assert.h.

Referenced by rb_sym2str().

## ◆ RUBY_ASSERT_FAIL

#define RUBY_ASSERT_FAIL | ( |  | mesg,
|  |  | ... )

Value:
rb_assert_failure##__VA_OPT__(_detail)( \
__FILE__, __LINE__, RBIMPL_ASSERT_FUNC, mesg RBIMPL_VA_OPT_ARGS(__VA_ARGS__))

Definition at line 164 of file assert.h.

## ◆ RUBY_ASSERT_MESG

#define RUBY_ASSERT_MESG | ( |  | expr,
|  |  | ... )

Value:
(RB_LIKELY(expr) ? RBIMPL_ASSERT_NOTHING : RUBY_ASSERT_FAIL(__VA_ARGS__))

Asserts that the expression is truthy.

If not aborts with the message.

Parameters

  expr | What supposedly evaluates to true.
  mesg | The message to display on failure.

Definition at line 186 of file assert.h.

## ◆ RUBY_ASSERT_MESG_WHEN

#define RUBY_ASSERT_MESG_WHEN | ( |  | cond,
|  |  | expr,
|  |  | ... )

Value:
((cond) ? RUBY_ASSERT_MESG((expr), __VA_ARGS__) : RBIMPL_ASSERT_NOTHING)

A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy.

Parameters

  cond | Extra condition that shall hold for assertion to take effect.
  expr | What supposedly evaluates to true.
  mesg | The message to display on failure.

Definition at line 262 of file assert.h.

## ◆ RUBY_ASSERT_NDEBUG

#define RUBY_ASSERT_NDEBUG | ( |  | ... | ) |

Value:
RBIMPL_ASSERT_NOTHING

A variant of RUBY_ASSERT that interfaces with NDEBUG instead of RUBY_DEBUG.

This almost resembles assert C standard macro, except minor implementation details.

Parameters

  expr | What supposedly evaluates to true.

Definition at line 236 of file assert.h.

## ◆ RUBY_ASSERT_WHEN

#define RUBY_ASSERT_WHEN | ( |  | cond,
|  |  | expr,
|  |  | ... )

Value:
RUBY_ASSERT_MESG_WHEN(cond, expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
RUBY_ASSERT_MESG_WHEN
#define RUBY_ASSERT_MESG_WHEN(cond, expr,...)
A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy.
Definition assert.h:262

A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or cond parameter is truthy.

Parameters

  cond | Extra condition that shall hold for assertion to take effect.
  expr | What supposedly evaluates to true.

Definition at line 278 of file assert.h.

## ◆ RUBY_DEBUG

#define RUBY_DEBUG 0

Define this macro when you want assertions.

Definition at line 88 of file assert.h.

Referenced by RSTRING_END(), and RSTRING_PTR().

## ◆ RUBY_NDEBUG

#define RUBY_NDEBUG 1

This macro is basically the same as NDEBUG.

Definition at line 92 of file assert.h.