Feature #14382 ยป 0001-Make-public-access-of-a-private-constant-call-const_.patch
test/ruby/test_module.rb | ||
---|---|---|
1388 | 1388 |
RUBY |
1389 | 1389 |
end |
1390 | 1390 | |
1391 |
def test_private_constant_const_missing |
|
1392 |
c = Class.new |
|
1393 |
c.const_set(:FOO, "foo") |
|
1394 |
c.private_constant(:FOO) |
|
1395 |
class << c |
|
1396 |
attr_reader :const_missing_arg |
|
1397 |
def const_missing(name) |
|
1398 |
@const_missing_arg = name |
|
1399 |
name == :FOO ? const_get(:FOO) : super |
|
1400 |
end |
|
1401 |
end |
|
1402 |
assert_equal("foo", c::FOO) |
|
1403 |
assert_equal(:FOO, c.const_missing_arg) |
|
1404 |
assert_raise(NameError) { c::BAR } |
|
1405 |
end |
|
1406 | ||
1407 |
def test_private_constant_nested_const_missing |
|
1408 |
c = Class.new |
|
1409 |
c.const_set(:FOO, "foo") |
|
1410 |
c.private_constant(:FOO) |
|
1411 |
sc = Class.new(c) |
|
1412 |
class << sc |
|
1413 |
attr_reader :const_missing_arg |
|
1414 |
attr_reader :const_missing_self |
|
1415 |
def const_missing(name) |
|
1416 |
@const_missing_arg = name |
|
1417 |
@const_missing_self = self |
|
1418 |
name == :FOO ? const_get(:FOO) : super |
|
1419 |
end |
|
1420 |
end |
|
1421 |
assert_equal("foo", sc::FOO) |
|
1422 |
assert_equal(:FOO, sc.const_missing_arg) |
|
1423 |
assert_equal(sc, sc.const_missing_self) |
|
1424 |
assert_raise(NameError) { sc::BAR } |
|
1425 |
end |
|
1426 | ||
1391 | 1427 |
class PrivateClass |
1392 | 1428 |
end |
1393 | 1429 |
private_constant :PrivateClass |
variable.c | ||
---|---|---|
1796 | 1796 |
rb_mod_const_missing(VALUE klass, VALUE name) |
1797 | 1797 |
{ |
1798 | 1798 |
rb_vm_pop_cfunc_frame(); |
1799 |
if (RB_TYPE_P(name, RUBY_T_SYMBOL)) { |
|
1800 |
rb_const_search(klass, SYM2ID(name), 0, 1, 2); |
|
1801 |
} |
|
1799 | 1802 |
uninitialized_constant(klass, name); |
1800 | 1803 | |
1801 | 1804 |
UNREACHABLE; |
... | ... | |
2362 | 2365 | |
2363 | 2366 |
while ((ce = rb_const_lookup(tmp, id))) { |
2364 | 2367 |
if (visibility && RB_CONST_PRIVATE_P(ce)) { |
2365 |
rb_name_err_raise("private constant %2$s::%1$s referenced", |
|
2366 |
tmp, ID2SYM(id)); |
|
2368 |
if (visibility == 2) { |
|
2369 |
rb_name_err_raise("private constant %2$s::%1$s referenced", |
|
2370 |
tmp, ID2SYM(id)); |
|
2371 |
} else { |
|
2372 |
return rb_const_missing(klass, ID2SYM(id)); |
|
2373 |
} |
|
2367 | 2374 |
} |
2368 | 2375 |
rb_const_warn_if_deprecated(ce, tmp, id); |
2369 | 2376 |
value = ce->value; |
2370 |
- |