Ruby: include/ruby/internal/intern/set.h File Reference

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

Functions
set.h File Reference

Public APIs related to rb_cSet. More...

`#include "ruby/internal/attr/nonnull.h"`
`#include "ruby/internal/dllexport.h"`
`#include "ruby/internal/value.h"`

Go to the source code of this file.

## Functions
void | rb_set_foreach (VALUE set, int(*func)(VALUE element, VALUE arg), VALUE arg)
| Iterates over a set.
VALUE | rb_set_new (void)
| Creates a new, empty set object.
VALUE | rb_set_new_capa (size_t capa)
| Identical to rb_set_new(), except it additionally specifies how many elements it is expected to contain.
bool | rb_set_lookup (VALUE set, VALUE element)
| Whether the set contains the given element.
bool | rb_set_add (VALUE set, VALUE element)
| Adds element to set.
VALUE | rb_set_clear (VALUE set)
| Removes all entries from set.
bool | rb_set_delete (VALUE set, VALUE element)
| Removes the element from from set.
size_t | rb_set_size (VALUE set)
| Returns the number of elements in the set.

## Detailed Description

Public APIs related to rb_cSet.

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

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 set.h.

## Function Documentation

## ◆ rb_set_add()

bool rb_set_add | ( | VALUE | set,
|  | VALUE | element )

Adds element to set.

Parameters

  [in] | set | Target set table to modify.
  [in] | element | Arbitrary Ruby object.

Exceptions

  rb_eFrozenError | `set` is frozen.

Returns
  true if element was not already in set, false otherwise

Postcondition
  element is in set.

Definition at line 1980 of file set.c.

Referenced by rb_set_add().

## ◆ rb_set_clear()

VALUE rb_set_clear | ( | VALUE | set | ) |

Removes all entries from set.

Parameters

  [out] | set | Target to clear.

Exceptions

  rb_eFrozenError | `set`is frozen.

Returns
  The passed set

Postcondition
  set has no elements.

Definition at line 1986 of file set.c.

Referenced by rb_set_clear().

## ◆ rb_set_delete()

bool rb_set_delete | ( | VALUE | set,
|  | VALUE | element )

Removes the element from from set.

Parameters

  [in] | set | Target set to modify.
  [in] | element | Key to delete.

Return values

  true | if element was already in set, false otherwise

Postcondition
  set does not have element as an element.

Definition at line 1992 of file set.c.

Referenced by rb_set_delete().

## ◆ rb_set_foreach()

void rb_set_foreach | ( | VALUE | set,
|  | int(* | func )(VALUE element, VALUE arg),
|  | VALUE | arg )

Iterates over a set.

Calls func with each element of the set and the argument given. func should return ST_CONTINUE, ST_STOP, or ST_DELETE.

Parameters

  [in] | set | An instance of rb_cSet to iterate over.
  [in] | func | Callback function to yield.
  [in] | arg | Passed as-is to func.

Exceptions

  rb_eRuntimeError | `set` was tampered during iterating.

Definition at line 1956 of file set.c.

Referenced by rb_set_foreach().

## ◆ rb_set_lookup()

bool rb_set_lookup | ( | VALUE | set,
|  | VALUE | element )

Whether the set contains the given element.

Parameters

  [in] | set | Set to look into.
  [in] | element | Set element to look for.

Returns
  true if element is in the set, falst otherwise.

Definition at line 1974 of file set.c.

Referenced by rb_set_lookup().

## ◆ rb_set_new()

VALUE rb_set_new | ( | void |  | ) |

Creates a new, empty set object.

Returns
  An allocated new instance of rb_cSet.

Definition at line 1962 of file set.c.

## ◆ rb_set_new_capa()

VALUE rb_set_new_capa | ( | size_t | capa | ) |

Identical to rb_set_new(), except it additionally specifies how many elements it is expected to contain.

This way you can create a set that is large enough for your need. For large sets, it means it won't need to be reallocated much, improving performance.

Parameters

  [in] | capa | Designed capacity of the set.

Returns
  An empty Set, whose capacity is capa.

Definition at line 1968 of file set.c.

Referenced by rb_set_new_capa().

## ◆ rb_set_size()

size_t rb_set_size | ( | VALUE | set | ) |

Returns the number of elements in the set.

Parameters

  [in] | set | A set object.

Returns
  The size of the set.

Definition at line 1998 of file set.c.

Referenced by rb_set_size().