Skip to content

Commit

Permalink
* eval.c (rb_mod_public_method_defined, etc.): new methods:
Browse files Browse the repository at this point in the history
  public_method_defined?, private_method_defined?,
  protected_method_defined?

* object.c (rb_obj_public_methods): new method
  Object#public_methods.

* class.c (ins_methods_i): Object#methods should list both public
  and protected methods.

* class.c (rb_class_public_instance_methods): new method
  Module#public_instance_methods.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3015 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Oct 30, 2002
1 parent a2868ff commit ed18815
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 10 deletions.
15 changes: 15 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
Wed Oct 30 17:00:47 2002 Yukihiro Matsumoto <[email protected]>

* eval.c (rb_mod_public_method_defined, etc.): new methods:
public_method_defined?, private_method_defined?,
protected_method_defined?

* object.c (rb_obj_public_methods): new method
Object#public_methods.

* class.c (ins_methods_i): Object#methods should list both public
and protected methods.

* class.c (rb_class_public_instance_methods): new method
Module#public_instance_methods.

Wed Oct 30 06:29:00 2002 Akinori MUSHA <[email protected]>

* eval.c, file.c, gc.c, io.c, object.c, ruby.c, ruby.h, struct.c,
Expand Down
53 changes: 46 additions & 7 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,23 +456,26 @@ rb_mod_ancestors(mod)
return ary;
}

#define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f))

static int
ins_methods_i(key, body, ary)
ID key;
NODE *body;
VALUE ary;
{
if ((body->nd_noex&(NOEX_PRIVATE|NOEX_PROTECTED)) == 0) {
if (!body->nd_body) {
rb_ary_push(ary, Qnil);
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
}
else if (!VISI_CHECK(body->nd_noex, NOEX_PRIVATE)) {
VALUE name = rb_str_new2(rb_id2name(key));

if (!rb_ary_includes(ary, name)) {
if (!body->nd_body) {
rb_ary_push(ary, Qnil);
}
rb_ary_push(ary, name);
}
}
else if (body->nd_body && nd_type(body->nd_body) == NODE_ZSUPER) {
else if (nd_type(body->nd_body) == NODE_ZSUPER) {
rb_ary_push(ary, Qnil);
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
}
Expand All @@ -489,7 +492,7 @@ ins_methods_prot_i(key, body, ary)
rb_ary_push(ary, Qnil);
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
}
else if (body->nd_noex & NOEX_PROTECTED) {
else if (VISI_CHECK(body->nd_noex, NOEX_PROTECTED)) {
VALUE name = rb_str_new2(rb_id2name(key));

if (!rb_ary_includes(ary, name)) {
Expand All @@ -513,7 +516,31 @@ ins_methods_priv_i(key, body, ary)
rb_ary_push(ary, Qnil);
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
}
else if (body->nd_noex & NOEX_PRIVATE) {
else if (VISI_CHECK(body->nd_noex, NOEX_PRIVATE)) {
VALUE name = rb_str_new2(rb_id2name(key));

if (!rb_ary_includes(ary, name)) {
rb_ary_push(ary, name);
}
}
else if (nd_type(body->nd_body) == NODE_ZSUPER) {
rb_ary_push(ary, Qnil);
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
}
return ST_CONTINUE;
}

static int
ins_methods_pub_i(key, body, ary)
ID key;
NODE *body;
VALUE ary;
{
if (!body->nd_body) {
rb_ary_push(ary, Qnil);
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
}
else if (VISI_CHECK(body->nd_noex, NOEX_PUBLIC)) {
VALUE name = rb_str_new2(rb_id2name(key));

if (!rb_ary_includes(ary, name)) {
Expand Down Expand Up @@ -590,6 +617,18 @@ rb_class_private_instance_methods(argc, argv, mod)
return method_list(mod, RTEST(option), ins_methods_priv_i);
}

VALUE
rb_class_public_instance_methods(argc, argv, mod)
int argc;
VALUE *argv;
VALUE mod;
{
VALUE option;

rb_scan_args(argc, argv, "01", &option);
return method_list(mod, RTEST(option), ins_methods_pub_i);
}

VALUE
rb_obj_singleton_methods(argc, argv, obj)
int argc;
Expand Down
51 changes: 49 additions & 2 deletions eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3510,8 +3510,52 @@ static VALUE
rb_mod_method_defined(mod, mid)
VALUE mod, mid;
{
if (rb_method_boundp(mod, rb_to_id(mid), 1)) {
return Qtrue;
return rb_method_boundp(mod, rb_to_id(mid), 1);
}

#define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f))

static VALUE
rb_mod_public_method_defined(mod, mid)
VALUE mod, mid;
{
VALUE klass;
ID id = rb_to_id(mid);
int noex;

if (rb_get_method_body(&mod, &id, &noex)) {
if (VISI_CHECK(noex, NOEX_PUBLIC))
return Qtrue;
}
return Qfalse;
}

static VALUE
rb_mod_private_method_defined(mod, mid)
VALUE mod, mid;
{
VALUE klass;
ID id = rb_to_id(mid);
int noex;

if (rb_get_method_body(&mod, &id, &noex)) {
if (VISI_CHECK(noex, NOEX_PRIVATE))
return Qtrue;
}
return Qfalse;
}

static VALUE
rb_mod_protected_method_defined(mod, mid)
VALUE mod, mid;
{
VALUE klass;
ID id = rb_to_id(mid);
int noex;

if (rb_get_method_body(&mod, &id, &noex)) {
if (VISI_CHECK(noex, NOEX_PROTECTED))
return Qtrue;
}
return Qfalse;
}
Expand Down Expand Up @@ -6152,6 +6196,9 @@ Init_eval()
rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, 1);
rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, 1);
rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, 1);
rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, 1);
rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval, -1);
Expand Down
1 change: 1 addition & 0 deletions intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ VALUE rb_mod_included_modules _((VALUE));
VALUE rb_mod_include_p _((VALUE, VALUE));
VALUE rb_mod_ancestors _((VALUE));
VALUE rb_class_instance_methods _((int, VALUE*, VALUE));
VALUE rb_class_public_instance_methods _((int, VALUE*, VALUE));
VALUE rb_class_protected_instance_methods _((int, VALUE*, VALUE));
VALUE rb_class_private_instance_methods _((int, VALUE*, VALUE));
VALUE rb_obj_singleton_methods _((int, VALUE*, VALUE));
Expand Down
1 change: 1 addition & 0 deletions node.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ typedef struct RNode {
#define NOEX_CFUNC 1
#define NOEX_PRIVATE 2
#define NOEX_PROTECTED 4
#define NOEX_MASK 6

NODE *rb_compile_cstr _((const char*, const char*, int, int));
NODE *rb_compile_string _((const char*, VALUE, int));
Expand Down
13 changes: 12 additions & 1 deletion object.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,16 @@ rb_obj_private_methods(obj)
return rb_class_private_instance_methods(1, argv, CLASS_OF(obj));
}

static VALUE
rb_obj_public_methods(obj)
VALUE obj;
{
VALUE argv[1];

argv[0] = Qtrue;
return rb_class_public_instance_methods(1, argv, CLASS_OF(obj));
}

static VALUE
convert_type(val, tname, method, raise)
VALUE val;
Expand Down Expand Up @@ -1329,6 +1339,7 @@ Init_Object()
rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1);
rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, 0);
rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, 0);
rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, 0);
rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0);
rb_define_private_method(rb_mKernel, "remove_instance_variable",
rb_obj_remove_instance_variable, 1);
Expand Down Expand Up @@ -1400,7 +1411,7 @@ Init_Object()
rb_define_singleton_method(rb_cModule, "allocate", rb_module_s_alloc, 0);
rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1);
rb_define_method(rb_cModule, "public_instance_methods", rb_class_instance_methods, -1);
rb_define_method(rb_cModule, "public_instance_methods", rb_class_public_instance_methods, -1);
rb_define_method(rb_cModule, "protected_instance_methods", rb_class_protected_instance_methods, -1);
rb_define_method(rb_cModule, "private_instance_methods", rb_class_private_instance_methods, -1);

Expand Down

0 comments on commit ed18815

Please sign in to comment.