Feature #14382 » 0001-Make-public-access-of-a-private-constant-call-const_.patch
test/ruby/test_module.rb | ||
---|---|---|
RUBY
|
||
end
|
||
def test_private_constant_const_missing
|
||
c = Class.new
|
||
c.const_set(:FOO, "foo")
|
||
c.private_constant(:FOO)
|
||
class << c
|
||
attr_reader :const_missing_arg
|
||
def const_missing(name)
|
||
@const_missing_arg = name
|
||
name == :FOO ? const_get(:FOO) : super
|
||
end
|
||
end
|
||
assert_equal("foo", c::FOO)
|
||
assert_equal(:FOO, c.const_missing_arg)
|
||
assert_raise(NameError) { c::BAR }
|
||
end
|
||
class PrivateClass
|
||
end
|
||
private_constant :PrivateClass
|
variable.c | ||
---|---|---|
rb_mod_const_missing(VALUE klass, VALUE name)
|
||
{
|
||
rb_vm_pop_cfunc_frame();
|
||
if (RB_TYPE_P(name, RUBY_T_SYMBOL)) {
|
||
rb_const_search(klass, SYM2ID(name), 0, 1, 2);
|
||
}
|
||
uninitialized_constant(klass, name);
|
||
UNREACHABLE;
|
||
... | ... | |
while ((ce = rb_const_lookup(tmp, id))) {
|
||
if (visibility && RB_CONST_PRIVATE_P(ce)) {
|
||
rb_name_err_raise("private constant %2$s::%1$s referenced",
|
||
tmp, ID2SYM(id));
|
||
if (visibility == 2) {
|
||
rb_name_err_raise("private constant %2$s::%1$s referenced",
|
||
tmp, ID2SYM(id));
|
||
} else {
|
||
return rb_const_missing(klass, ID2SYM(id));
|
||
}
|
||
}
|
||
rb_const_warn_if_deprecated(ce, tmp, id);
|
||
value = ce->value;
|