Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,15 @@ ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
return ins_methods_type_i(name, type, ary, METHOD_VISI_PUBLIC);
}

static int
ins_methods_undef_i(st_data_t name, st_data_t type, st_data_t ary)
{
if ((rb_method_visibility_t)type == METHOD_VISI_UNDEF) {
ins_methods_push(name, ary);
}
return ST_CONTINUE;
}

struct method_entry_arg {
st_table *list;
int recur;
Expand Down Expand Up @@ -1783,6 +1792,21 @@ rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
}

/*
* call-seq:
* mod.undefined_instance_methods(include_super=true) -> array
*
* Returns a list of the undefined instance methods defined in <i>mod</i>.
* If the optional parameter is <code>false</code>, the methods of
* any ancestors are not included.
*/

VALUE
rb_class_undefined_instance_methods(int argc, const VALUE *argv, VALUE mod)
{
return class_instance_method_list(argc, argv, mod, 0, ins_methods_undef_i);
}

/*
* call-seq:
* obj.methods(regular=true) -> array
Expand Down Expand Up @@ -1868,6 +1892,21 @@ rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
}

/*
* call-seq:
* obj.undefined_methods(all=true) -> array
*
* Returns the list of undefined methods accessible to <i>obj</i>. If
* the <i>all</i> parameter is set to <code>false</code>, only those methods
* in the receiver will be listed.
*/

VALUE
rb_obj_undefined_methods(int argc, const VALUE *argv, VALUE obj)
{
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_undef_i);
}

/*
* call-seq:
* obj.singleton_methods(all=true) -> array
Expand Down Expand Up @@ -2119,6 +2158,46 @@ rb_singleton_class(VALUE obj)
return klass;
}

/*!
* Returns the singleton instance of \a klass, or nil if \a klass is
* not a singleton class.
*
* \param klass a singleton class.
* \throw TypeError if \a obj is a Fixnum or a Symbol.
* \return the singleton class.
*
* \post \a obj has its own singleton class.
* \post if \a obj is a class,
* the returned singleton class also has its own
* singleton class in order to keep consistency of the
* inheritance structure of metaclasses.
* \note a new singleton class will be created
* if \a obj does not have it.
* \note the singleton classes for nil, true and false are:
* NilClass, TrueClass and FalseClass.
*/

/*
* call-seq:
* class.singleton_instance -> object or nil
*
* Returns the singleton instance of <i>class</i>, or <code>nil</code>.
*/
VALUE
rb_class_singleton_instance(VALUE klass)
{
VALUE obj;

Check_Type(klass, T_CLASS);
if (klass == rb_cNilClass) return Qnil;
if (klass == rb_cFalseClass) return Qfalse;
if (klass == rb_cTrueClass) return Qtrue;
if (!FL_TEST_RAW(klass, FL_SINGLETON)) return Qnil;
obj = rb_attr_get(klass, id_attached);
if (SPECIAL_CONST_P(obj) || RBASIC_CLASS(obj) != klass) return Qnil;
return obj;
}

/*!
* \}
*/
Expand Down
3 changes: 3 additions & 0 deletions internal/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
VALUE rb_obj_undefined_methods(int argc, const VALUE *argv, VALUE obj);
VALUE rb_class_undefined_instance_methods(int argc, const VALUE *argv, VALUE klass);
VALUE rb_class_singleton_instance(VALUE klass);
VALUE rb_special_singleton_class(VALUE);
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
VALUE rb_singleton_class_get(VALUE obj);
Expand Down
4 changes: 4 additions & 0 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -4519,6 +4519,7 @@ InitVM_Object(void)
rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
rb_define_method(rb_mKernel, "undefined_methods", rb_obj_undefined_methods, -1); /* in class.c */
rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
Expand Down Expand Up @@ -4588,6 +4589,8 @@ InitVM_Object(void)
rb_class_protected_instance_methods, -1); /* in class.c */
rb_define_method(rb_cModule, "private_instance_methods",
rb_class_private_instance_methods, -1); /* in class.c */
rb_define_method(rb_cModule, "undefined_instance_methods",
rb_class_undefined_instance_methods, -1); /* in class.c */

rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
Expand Down Expand Up @@ -4616,6 +4619,7 @@ InitVM_Object(void)
rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
rb_define_method(rb_cClass, "subclasses", rb_class_subclasses, 0); /* in class.c */
rb_define_method(rb_cClass, "singleton_instance", rb_class_singleton_instance, 0); /* in class.c */
rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
rb_undef_method(rb_cClass, "extend_object");
rb_undef_method(rb_cClass, "append_features");
Expand Down
5 changes: 5 additions & 0 deletions test/ruby/test_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,11 @@ def test_singleton_class_p
assert_not_predicate(self.class, :singleton_class?, feature7609)
end

def test_singleton_instance
obj = Object.new
assert_same(obj, obj.singleton_class.singleton_instance)
end

def test_freeze_to_s
assert_nothing_raised("[ruby-core:41858] [Bug #5828]") {
Class.new.freeze.clone.to_s
Expand Down
10 changes: 10 additions & 0 deletions test/ruby/test_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,16 @@ def test_public_instance_methods
assert_equal([:bClass1], BClass.public_instance_methods(false))
end

def test_undefined_instance_methods
assert_equal([], AClass.undefined_instance_methods(false))
assert_equal([], BClass.undefined_instance_methods(false))
c = Class.new(AClass) {undef aClass}
assert_equal([:aClass], c.undefined_instance_methods(false))
c = Class.new(c)
assert_equal([], c.undefined_instance_methods(false))
assert_equal([:aClass], c.undefined_instance_methods(true))
end

def test_s_public
o = (c = Class.new(AClass)).new
assert_raise(NoMethodError, /private method/) {o.aClass1}
Expand Down
11 changes: 11 additions & 0 deletions test/ruby/test_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ def test_methods2

assert_equal([:foo, :foo2], (o2.public_methods - o0.public_methods).sort)
assert_equal([:foo2], (o2.public_methods(false) - o0.public_methods(false)).sort)

assert_equal([], (o2.undefined_methods - o0.undefined_methods).sort)
c3 = Class.new(c2) do
undef foo
end
assert_equal([:foo], (c3.undefined_instance_methods - c2.undefined_instance_methods).sort)
c4 = Class.new(c3) do
undef foo2
end
assert_equal([:foo, :foo2], (c4.undefined_instance_methods - c2.undefined_instance_methods).sort)
assert_equal([:foo2], (c4.undefined_instance_methods(false) - c2.undefined_instance_methods).sort)
end

def test_methods_prepend
Expand Down