[Ironruby-core] object equality

William Green will at hotgazpacho.org
Thu Sep 9 06:20:15 EDT 2010


Testing for object equality in C# is different than it is in Ruby. In
C#, you need to override both Object.Equals and Object.GetHashCode (I
forget which is used when, but I do recall that the compiler complains
if you override one and not the other). So, when you bring your Ruby
object into C# and compare them, C# doesn't see an override for Equals
on your object, and thus uses Object.Equals (which is often what you
don't want). Try defining an equals method on your Ruby object, or
alias it to ==.

I suspect that the == method on your Ruby object does not map to
Equals when you bring into C#. And I'm not sure that it should.

Thoughts?

--
Will Green
http://hotgazpacho.org/



On Sep 9, 2010, at 4:33 AM, Davy Brion <ralinx at davybrion.com> wrote:

> If i have the following class in ruby:
>
> class TestClass
>   def initialize(value)
>     @value = value
>   end
>
>   def ==(other)
>     return false if other.nil?
>     self.value == other.value
>   end
>
>   protected
>
>   def value
>     @value
>   end
> end
>
> test1 = TestClass.new(5)
> test2 = TestClass.new(5)
> p test1 == test2
> p test1 != test2
>
> the output is:
> true
> false
>
> if i do this in .NET:
>
> dynamic test1 = ruby.TestClass. at new(5);
> dynamic test2 = ruby.TestClass. at new(5);
>
> var equals = test1 == test2;
> var differs = test1 != test2;
>
> both equals and differs are true
>
> i'm going to create an issue about this, but i do need to get this working... is there a temporary workaround that i can use for now?
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core


More information about the Ironruby-core mailing list