class Namespace
Public Class Methods
Source
static VALUE
rb_namespace_current(VALUE klass)
{
const rb_namespace_t *ns = rb_current_namespace();
if (NAMESPACE_USER_P(ns)) {
return ns->ns_object;
}
if (NAMESPACE_BUILTIN_P(ns)) {
return Qnil;
}
return Qfalse;
}
Source
static VALUE
rb_namespace_s_getenabled(VALUE namespace)
{
return RBOOL(rb_namespace_available());
}
Source
static VALUE
rb_namespace_s_is_builtin_p(VALUE namespace, VALUE klass)
{
if (RCLASS_PRIME_CLASSEXT_READABLE_P(klass) && !RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass))
return Qtrue;
return Qfalse;
}
Source
static VALUE
namespace_initialize(VALUE namespace)
{
rb_namespace_t *ns;
rb_classext_t *object_classext;
VALUE entry;
ID id_namespace_entry;
CONST_ID(id_namespace_entry, "__namespace_entry__");
if (!rb_namespace_available()) {
rb_raise(rb_eRuntimeError, "Namespace is disabled. Set RUBY_NAMESPACE=1 environment variable to use Namespace.");
}
entry = rb_class_new_instance_pass_kw(0, NULL, rb_cNamespaceEntry);
ns = get_namespace_struct_internal(entry);
ns->ns_object = namespace;
ns->ns_id = namespace_generate_id();
ns->load_path = rb_ary_dup(GET_VM()->load_path);
ns->is_user = true;
rb_define_singleton_method(ns->load_path, "resolve_feature_path", rb_resolve_feature_path, 1);
// Set the Namespace object unique/consistent from any namespaces to have just single
// constant table from any view of every (including main) namespace.
// If a code in the namespace adds a constant, the constant will be visible even from root/main.
RCLASS_SET_PRIME_CLASSEXT_WRITABLE(namespace, true);
// Get a clean constant table of Object even by writable one
// because ns was just created, so it has not touched any constants yet.
object_classext = RCLASS_EXT_WRITABLE_IN_NS(rb_cObject, ns);
RCLASS_SET_CONST_TBL(namespace, RCLASSEXT_CONST_TBL(object_classext), true);
rb_ivar_set(namespace, id_namespace_entry, entry);
setup_pushing_loading_namespace(ns);
return namespace;
}
Public Instance Methods
Source
static VALUE
rb_namespace_eval(VALUE namespace, VALUE str)
{
rb_thread_t *th = GET_THREAD();
StringValue(str);
namespace_push(th, namespace);
return rb_ensure(rb_namespace_eval_string, str, namespace_pop, (VALUE)th);
}
Source
static VALUE
rb_namespace_inspect(VALUE obj)
{
rb_namespace_t *ns;
VALUE r;
if (obj == Qfalse) {
r = rb_str_new_cstr("#<Namespace:root>");
return r;
}
ns = rb_get_namespace_t(obj);
r = rb_str_new_cstr("#<Namespace:");
rb_str_concat(r, rb_funcall(LONG2NUM(ns->ns_id), rb_intern("to_s"), 0));
if (NAMESPACE_BUILTIN_P(ns)) {
rb_str_cat_cstr(r, ",builtin");
}
if (NAMESPACE_USER_P(ns)) {
rb_str_cat_cstr(r, ",user");
}
if (NAMESPACE_MAIN_P(ns)) {
rb_str_cat_cstr(r, ",main");
}
else if (NAMESPACE_OPTIONAL_P(ns)) {
rb_str_cat_cstr(r, ",optional");
}
rb_str_cat_cstr(r, ">");
return r;
}
Source
static VALUE
rb_namespace_load(int argc, VALUE *argv, VALUE namespace)
{
VALUE fname, wrap;
rb_thread_t *th = GET_THREAD();
rb_namespace_t *ns = rb_get_namespace_t(namespace);
rb_scan_args(argc, argv, "11", &fname, &wrap);
VALUE args = rb_ary_new_from_args(2, fname, wrap);
namespace_push(th, namespace);
rb_namespace_push_loading_namespace(ns);
struct namespace_pop2_arg arg = {
.th = th,
.ns = ns
};
return rb_ensure(rb_load_entrypoint, args, namespace_both_pop, (VALUE)&arg);
}
Source
static VALUE
rb_namespace_load_path(VALUE namespace)
{
return rb_get_namespace_t(namespace)->load_path;
}
Source
static VALUE
rb_namespace_require(VALUE namespace, VALUE fname)
{
rb_thread_t *th = GET_THREAD();
rb_namespace_t *ns = rb_get_namespace_t(namespace);
namespace_push(th, namespace);
rb_namespace_push_loading_namespace(ns);
struct namespace_pop2_arg arg = {
.th = th,
.ns = ns
};
return rb_ensure(rb_require_string, fname, namespace_both_pop, (VALUE)&arg);
}
Source
static VALUE
rb_namespace_require_relative(VALUE namespace, VALUE fname)
{
rb_thread_t *th = GET_THREAD();
rb_namespace_t *ns = rb_get_namespace_t(namespace);
namespace_push(th, namespace);
rb_namespace_push_loading_namespace(ns);
struct namespace_pop2_arg arg = {
.th = th,
.ns = ns
};
return rb_ensure(rb_require_relative_entrypoint, fname, namespace_both_pop, (VALUE)&arg);
}