In your example, you're not overriding the sealed method (since it is sealed 
:)). You're defining a new method on the subclass. You can do this in C# too:

class NewSquere : Square {
  public new int GetNumberOfSizes() {
    return 99;
  }
}

As the "new" keyword suggest this method doesn't share the v-table slot with 
the base class method - it has its own new slot. Hence no virtual call on 
NewSquare instance strongly typed to Shape or Square will call your method.

Tomas

From: [email protected] 
[mailto:[email protected]] On Behalf Of Mark Ryall
Sent: Saturday, June 13, 2009 1:37 AM
To: [email protected]
Subject: Re: [Ironruby-core] Sealed method can be overriden

You'll be able to override anything in ruby - you'll find CLR classes harder to 
fool.

Try creating a C# method that accepts a Shape and calls the GetNumberOfSizes() 
method - I expect you'll find that the NewSquare override is ignored and the 
Square method is called instead.

I was surprised to find that a non virtual property in a C# class was able to 
be replaced by defining a new one in ruby (this was not the case for a non 
virtual method) - see 
http://github.com/markryall/orangutan/blob/412913de26b9d545ebf08b2a1367e6675f8625f7/spikes/experiment1.rb

Mark.

On Sat, Jun 13, 2009 at 6:22 PM, Shay Friedman 
<[email protected]<mailto:[email protected]>> wrote:
Hi guys,

I'm trying to override a sealed method in IR and I succeed.

For example:
C#:
public class Shape
{
 public virtual int GetNumberOfSizes() { return 0; }
}
public class Square : Shape
{
 public override sealed int GetNumberOfSizes() { return 4; }
}

IR:
class NewSquare < Square
 def get_number_of_sizes
   return 99
 end
end
puts NewSquare.new.get_number_of_sizes # => 99

Bug or by design?

Thanks,
Shay.
----------------------------
Shay Friedman
http://www.ironshay.com
Follow me: http://twitter.com/ironshay
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ironruby-core mailing list
[email protected]<mailto:[email protected]>
http://rubyforge.org/mailman/listinfo/ironruby-core

_______________________________________________
Ironruby-core mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core

Reply via email to