Symbol¶
Rice::Symbol wraps Ruby's Symbol class. Symbols are internal identifiers in Ruby - they are singletons and can be thought of as frozen strings. They differ from an Identifier in that they are real Ruby Objects, but they can be converted back and forth between Identifier and Symbol.
Constructors¶
Symbol(VALUE v)¶
Wrap an existing Ruby symbol VALUE.
Parameters:
v- A Ruby VALUE of typeT_SYMBOL.
Throws:
TypeError if the value is not a symbol.
VALUE ruby_sym = rb_intern("my_symbol");
VALUE sym_value = rb_id2sym(ruby_sym);
Symbol s(sym_value);
Symbol(Object v)¶
Wrap an existing Ruby Object that is a symbol.
Parameters:
v- An Object wrapping a symbol VALUE.
Throws:
TypeError if the value is not a symbol.
Symbol(Identifier id)¶
Construct a Symbol from an Identifier.
Parameters:
id- An Identifier to convert to a Symbol.
Symbol(char const* s = "")¶
Construct a Symbol from a null-terminated C string.
Parameters:
s- A null-terminated C string (default: empty string).
Symbol(std::string const& s)¶
Construct a Symbol from a C++ string.
Parameters:
s- A C++ std::string.
Symbol(std::string_view const& s)¶
Construct a Symbol from a string_view.
Parameters:
s- A C++ std::string_view.
Public Methods¶
c_str() const -> char const*¶
Get a pointer to the symbol's string representation.
Returns:
A pointer to the null-terminated string.
Warning:
The pointer is only valid while the Symbol object exists.
str() const -> std::string¶
Get a copy of the symbol as a C++ string.
Returns:
A std::string copy of the symbol's string representation.
to_id() const -> Identifier¶
Convert the Symbol to an Identifier.
Returns:
An Identifier representing the same symbol.
Inherited Methods¶
Symbol inherits all methods from Object, including:
value()- Get the underlying VALUEto_s()- Convert to string representationinspect()- Get detailed representation (e.g.,:symbol)call()- Call Ruby methodsis_nil()- Check if nil
Example¶
#include <rice/rice.hpp>
#include <iostream>
using namespace Rice;
void example()
{
// Create symbols from various sources
Symbol s1("name");
Symbol s2(std::string("age"));
Symbol s3(Identifier("active"));
// Get string representations
std::cout << s1.str() << std::endl; // "name"
std::cout << s1.c_str() << std::endl; // "name"
// Convert to Identifier
Identifier id = s1.to_id();
// Use as hash keys (common use case)
Hash h;
h[Symbol("name")] = "Alice";
h[Symbol("age")] = 30;
// Compare symbols (they are singletons)
Symbol s4("name");
if (s1.value() == s4.value()) {
std::cout << "Same symbol!" << std::endl;
}
// Call Ruby methods
Object upper = s1.call("to_s").call("upcase"); // "NAME"
Object inspected = s1.inspect(); // ":name"
}
// Exposing a method that takes a Symbol parameter
extern "C"
void Init_my_extension()
{
define_module("MyModule")
.define_module_function("process", [](Symbol action) {
std::string action_str = action.str();
if (action_str == "start") {
return "Starting...";
} else if (action_str == "stop") {
return "Stopping...";
}
return "Unknown action";
});
// Ruby usage:
// MyModule.process(:start) #=> "Starting..."
// MyModule.process(:stop) #=> "Stopping..."
}
See Also¶
- Object - Base class
- Identifier - Internal identifier (not a Ruby Object)
- String - String wrapper class