summaryrefslogtreecommitdiff
path: root/proc.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-10-27 15:36:00 -0700
committerJeremy Evans <code@jeremyevans.net>2021-12-09 12:59:37 -0800
commit27278150685e738f84105d09843d3ba371146c7a (patch)
tree2999a2683e67b9c63a9d44faac52e2d67152ee37 /proc.c
parent74159f7f3e58d8e5ef2e6ee430b7ffa2ade5d952 (diff)
Add {Method,UnboundMethod}#{public?,private?,protected?}
These methods allow for checking whether the method has that visibility. Implements [Feature #11689]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5040
Diffstat (limited to 'proc.c')
-rw-r--r--proc.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/proc.c b/proc.c
index 94b269d694..90ecf1e59b 100644
--- a/proc.c
+++ b/proc.c
@@ -3228,6 +3228,51 @@ method_super_method(VALUE method)
}
/*
+ * call-seq:
+ * meth.public? -> true or false
+ *
+ * Returns whether the method is public.
+ */
+
+static VALUE
+method_public_p(VALUE method)
+{
+ const struct METHOD *data;
+ TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
+ return RBOOL(METHOD_ENTRY_VISI(data->me) == METHOD_VISI_PUBLIC);
+}
+
+/*
+ * call-seq:
+ * meth.protected? -> true or false
+ *
+ * Returns whether the method is protected.
+ */
+
+static VALUE
+method_protected_p(VALUE method)
+{
+ const struct METHOD *data;
+ TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
+ return RBOOL(METHOD_ENTRY_VISI(data->me) == METHOD_VISI_PROTECTED);
+}
+
+/*
+ * call-seq:
+ * meth.private? -> true or false
+ *
+ * Returns whether the method is private.
+ */
+
+static VALUE
+method_private_p(VALUE method)
+{
+ const struct METHOD *data;
+ TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
+ return RBOOL(METHOD_ENTRY_VISI(data->me) == METHOD_VISI_PRIVATE);
+}
+
+/*
* call-seq:
* local_jump_error.exit_value -> obj
*
@@ -4163,6 +4208,9 @@ Init_Proc(void)
rb_define_method(rb_cMethod, "source_location", rb_method_location, 0);
rb_define_method(rb_cMethod, "parameters", rb_method_parameters, 0);
rb_define_method(rb_cMethod, "super_method", method_super_method, 0);
+ rb_define_method(rb_cMethod, "public?", method_public_p, 0);
+ rb_define_method(rb_cMethod, "protected?", method_protected_p, 0);
+ rb_define_method(rb_cMethod, "private?", method_private_p, 0);
rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);
rb_define_method(rb_mKernel, "singleton_method", rb_obj_singleton_method, 1);
@@ -4186,6 +4234,9 @@ Init_Proc(void)
rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0);
rb_define_method(rb_cUnboundMethod, "parameters", rb_method_parameters, 0);
rb_define_method(rb_cUnboundMethod, "super_method", method_super_method, 0);
+ rb_define_method(rb_cUnboundMethod, "public?", method_public_p, 0);
+ rb_define_method(rb_cUnboundMethod, "protected?", method_protected_p, 0);
+ rb_define_method(rb_cUnboundMethod, "private?", method_private_p, 0);
/* Module#*_method */
rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);