[macruby-changes] [4063] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon May 10 18:19:53 PDT 2010


Revision: 4063
          http://trac.macosforge.org/projects/ruby/changeset/4063
Author:   lsansonetti at apple.com
Date:     2010-05-10 18:19:52 -0700 (Mon, 10 May 2010)
Log Message:
-----------
make NSObject#== and NSObject#eql? use -[NSObject isEqual:]

Modified Paths:
--------------
    MacRuby/trunk/objc.h
    MacRuby/trunk/objc.m
    MacRuby/trunk/object.c
    MacRuby/trunk/spec/macruby/core/hash_spec.rb
    MacRuby/trunk/spec/macruby/core/object_spec.rb

Modified: MacRuby/trunk/objc.h
===================================================================
--- MacRuby/trunk/objc.h	2010-05-11 00:52:12 UTC (rev 4062)
+++ MacRuby/trunk/objc.h	2010-05-11 01:19:52 UTC (rev 4063)
@@ -260,6 +260,7 @@
 void rb_objc_exception_raise(const char *name, const char *message);
 
 bool rb_objc_ignore_sel(SEL sel);
+bool rb_objc_isEqual(VALUE x, VALUE y); 
 void rb_objc_force_class_initialize(Class klass);
 void rb_objc_fix_relocatable_load_path(void);
 

Modified: MacRuby/trunk/objc.m
===================================================================
--- MacRuby/trunk/objc.m	2010-05-11 00:52:12 UTC (rev 4062)
+++ MacRuby/trunk/objc.m	2010-05-11 01:19:52 UTC (rev 4063)
@@ -630,6 +630,12 @@
 	|| sel == @selector(dealloc);
 }
 
+bool
+rb_objc_isEqual(VALUE x, VALUE y)
+{
+    return [RB2OC(x) isEqual:RB2OC(y)];
+}
+
 void
 rb_objc_force_class_initialize(Class klass)
 {

Modified: MacRuby/trunk/object.c
===================================================================
--- MacRuby/trunk/object.c	2010-05-11 00:52:12 UTC (rev 4062)
+++ MacRuby/trunk/object.c	2010-05-11 01:19:52 UTC (rev 4063)
@@ -125,6 +125,12 @@
     return obj1 == obj2 ? Qtrue : Qfalse;
 }
 
+static VALUE
+rb_nsobj_equal(VALUE obj1, SEL sel, VALUE obj2)
+{
+    return rb_objc_isEqual(obj1, obj2) ? Qtrue : Qfalse;
+}
+
 /*
  *  call-seq:
  *     !obj    => true or false
@@ -2983,8 +2989,9 @@
     rb_objc_define_direct_method(*(VALUE *)rb_cNSObject, "new:", rb_class_new_instance_imp, -1);
 
     rb_objc_define_private_method(rb_cNSObject, "initialize", rb_obj_dummy, 0);
-    rb_objc_define_method(rb_cNSObject, "==", rb_obj_equal, 1);
+    rb_objc_define_method(rb_cRubyObject, "==", rb_obj_equal, 1);
     rb_objc_define_method(rb_cNSObject, "equal?", rb_obj_equal, 1);
+    rb_objc_define_method(rb_cNSObject, "==", rb_nsobj_equal, 1);
     rb_objc_define_method(rb_cNSObject, "!", rb_obj_not, 0);
     rb_objc_define_method(rb_cNSObject, "!=", rb_obj_not_equal, 1);
 
@@ -3017,6 +3024,7 @@
     rb_objc_define_method(rb_mKernel, "=~", rb_obj_match, 1);
     rb_objc_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
     rb_objc_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
+    rb_objc_define_method(rb_cNSObject, "eql?", rb_nsobj_equal, 1);
     rb_objc_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
 
     rb_objc_define_method(rb_cNSObject, "clone", rb_obj_clone_imp, 0);

Modified: MacRuby/trunk/spec/macruby/core/hash_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/core/hash_spec.rb	2010-05-11 00:52:12 UTC (rev 4062)
+++ MacRuby/trunk/spec/macruby/core/hash_spec.rb	2010-05-11 01:19:52 UTC (rev 4063)
@@ -44,6 +44,16 @@
     a.foo = 42
     a.foo.should == 42
   end
+
+  it "properly hashes pure Cocoa objects" do
+    a = NSCalendarDate.dateWithYear(2010, month:5, day:2, hour:0,  minute:0, second:0, timeZone:nil)
+    b = NSCalendarDate.dateWithYear(2010, month:5, day:2, hour:0,  minute:0, second:0, timeZone:nil)
+    h = {}
+    h[a] = :bad
+    h[b] = :ok
+    h.size.should == 1
+    h[a].should == :ok
+  end
 end
 
 describe "An NSDictionary object" do

Modified: MacRuby/trunk/spec/macruby/core/object_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/core/object_spec.rb	2010-05-11 00:52:12 UTC (rev 4062)
+++ MacRuby/trunk/spec/macruby/core/object_spec.rb	2010-05-11 01:19:52 UTC (rev 4063)
@@ -72,4 +72,13 @@
     o.foo.should == 42
     o.bar.should == 42
   end
+
+  it "has its isEqual: method used for #== and #eql?" do
+    a = NSCalendarDate.dateWithYear(2010, month:5, day:2, hour:0,  minute:0, second:0, timeZone:nil)
+    b = NSCalendarDate.dateWithYear(2010, month:5, day:2, hour:0,  minute:0, second:0, timeZone:nil)
+    a.isEqual(b).should == true
+    (a == b).should == true
+    a.eql?(b).should == true
+    a.equal?(b).should == false
+  end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100510/8b01a26b/attachment.html>


More information about the macruby-changes mailing list