Skip to content
Browse files

Merge pull request #1098 from jeffkreeftmeijer/master

Add Object#rb_class_inherited_p
  • Loading branch information...
2 parents c07aba4 + fd17863 commit 151932afeb67a44ab1cb54b6a57797c46a9274f3 @dbussink dbussink committed Aug 5, 2011
View
11 spec/ruby/optional/capi/ext/object_spec.c
@@ -323,6 +323,13 @@ static VALUE object_spec_rb_equal(VALUE self, VALUE a, VALUE b) {
}
#endif
+#ifdef HAVE_RB_CLASS_INHERITED_P
+static VALUE object_spec_rb_class_inherited_p(VALUE self, VALUE mod, VALUE arg) {
+ return rb_class_inherited_p(mod, arg);
+}
+#endif
+
+
void Init_object_spec() {
VALUE cls;
cls = rb_define_class("CApiObjectSpecs", rb_cObject);
@@ -471,6 +478,10 @@ void Init_object_spec() {
rb_define_method(cls, "rb_equal", object_spec_rb_equal, 2);
#endif
+#ifdef HAVE_RB_CLASS_INHERITED_P
+ rb_define_method(cls, "rb_class_inherited_p", object_spec_rb_class_inherited_p, 2);
+#endif
+
#ifdef HAVE_RB_OBJ_INSTANCE_EVAL
rb_define_method(cls, "rb_obj_instance_eval", object_spec_rb_obj_instance_eval, 1);
#endif
View
1 spec/ruby/optional/capi/ext/rubyspec.h
@@ -236,6 +236,7 @@
#define HAVE_RB_CLASS_OF 1
#define HAVE_RB_CONVERT_TYPE 1
#define HAVE_RB_EQUAL 1
+#define HAVE_RB_CLASS_INHERITED_P 1
#define HAVE_RB_EXTEND_OBJECT 1
#define HAVE_RB_INSPECT 1
#define HAVE_RB_IVAR_DEFINED 1
View
22 spec/ruby/optional/capi/object_spec.rb
@@ -428,6 +428,28 @@ def reach
end
end
+ describe "rb_class_inherited_p" do
+
+ it "returns true if mod equals arg" do
+ @o.rb_class_inherited_p(Array, Array).should be_true
+ end
+
+ it "returns true if mod is a subclass of arg" do
+ @o.rb_class_inherited_p(Array, Object).should be_true
+ end
+
+ it "returns nil if mod is not a subclass of arg" do
+ @o.rb_class_inherited_p(Array, Hash).should be_nil
+ end
+
+ it "raises a TypeError if arg is no class or module" do
+ lambda{
+ @o.rb_class_inherited_p(1, 2)
+ }.should raise_error(TypeError)
+ end
+
+ end
+
describe "instance variable access" do
before do
@test = ObjectTest.new
View
1 vm/capi/include/ruby.h
@@ -748,6 +748,7 @@ VALUE rb_uint2big(unsigned long number);
int rb_cmpint(VALUE val, VALUE a, VALUE b);
void rb_cmperr(VALUE x, VALUE y);
VALUE rb_equal(VALUE a, VALUE b);
+ VALUE rb_class_inherited_p(VALUE mod, VALUE arg);
#define Data_Make_Struct(klass, type, mark, free, sval) (\
sval = ALLOC(type), \
View
30 vm/capi/object.cpp
@@ -43,10 +43,10 @@ extern "C" {
int type;
const char *name;
} builtin_types[] = {
- {T_NIL, "nil"},
- {T_OBJECT, "Object"},
- {T_CLASS, "Class"},
- {T_ICLASS, "iClass"}, /* internal use: mixed-in module holder */
+ {T_NIL, "nil"},
+ {T_OBJECT, "Object"},
+ {T_CLASS, "Class"},
+ {T_ICLASS, "iClass"}, /* internal use: mixed-in module holder */
{T_MODULE, "Module"},
{T_FLOAT, "Float"},
{T_STRING, "String"},
@@ -59,13 +59,13 @@ extern "C" {
{T_FILE, "File"},
{T_TRUE, "true"},
{T_FALSE, "false"},
- {T_SYMBOL, "Symbol"}, /* :symbol */
- {T_DATA, "Data"}, /* internal use: wrapped C pointers */
- {T_MATCH, "MatchData"}, /* data of $~ */
- {T_VARMAP, "Varmap"}, /* internal use: dynamic variables */
- {T_SCOPE, "Scope"}, /* internal use: variable scope */
- {T_NODE, "Node"}, /* internal use: syntax tree node */
- {T_UNDEF, "undef"}, /* internal use: #undef; should not happen */
+ {T_SYMBOL, "Symbol"}, /* :symbol */
+ {T_DATA, "Data"}, /* internal use: wrapped C pointers */
+ {T_MATCH, "MatchData"}, /* data of $~ */
+ {T_VARMAP, "Varmap"}, /* internal use: dynamic variables */
+ {T_SCOPE, "Scope"}, /* internal use: variable scope */
+ {T_NODE, "Node"}, /* internal use: syntax tree node */
+ {T_UNDEF, "undef"}, /* internal use: #undef; should not happen */
{-1, 0}
};
@@ -377,6 +377,14 @@ extern "C" {
return Qfalse;
}
+ VALUE rb_class_inherited_p(VALUE mod, VALUE arg) {
+ if(TYPE(arg) != T_MODULE && TYPE(arg) != T_CLASS) {
+ rb_raise(rb_eTypeError, "compared with non class/module");
+ }
+
+ return rb_funcall(mod, rb_intern("<="), 1, arg);
+ }
+
int rb_obj_respond_to(VALUE obj, ID method_name, int priv) {
VALUE include_private = priv == 1 ? Qtrue : Qfalse;
return RTEST(rb_funcall(obj, rb_intern("respond_to?"), 2, ID2SYM(method_name), include_private));

0 comments on commit 151932a

Please sign in to comment.
Something went wrong with that request. Please try again.