Posts

Showing posts from January, 2012

Ruby Open Classes

Coming from the .NET world, if we ever wanted to improve a class (by adding to it's code), we really only had a couple of options: Subclass This is limiting because .NET is a single-inheritence model. This means a class can only be subclassed once. So if other developers want to add more functionality, you can end in an inheritance chain nightmare. Extension Methods Since .NET 3.0, we've had extension methods . These allow you to bolt your own methods on to a type - the one gotcha being that they must be static. This would mean that you could only get at any public members of the instance you are executed against. (I should note, these are not criticisms of the language. Just because I have jumped ship to Ruby, I still consider .NET to be a top-notch framework) Enter "Open Classes" In Ruby, we have what are called " Open Classes " - and they are pretty much what they say on the tin. Classes are open for extension and modification (yups, dire