Fun With Extend in IronRuby
Building on the implements idea from the last post, I’ve been playing around with extending CLR objects on the fly as I bring them at runtime. I wrote a quickie load_assemblies method that checks objects to see if they implement an interface, and extend them if they do.
I think this pattern will be very useful when importing existing .NET objects into IronRuby. For instance, if you wanted all of the objects governed by your ORM to take on ActiveRecord-like behavior, you could catch them and extend them like this:
class Class
def implements? interface
!to_clr_type.nil? && !to_clr_type.get_interface(interface).nil?
end
end
def load_assemblies(assemblies)
assemblies.each do |assembly|
require assembly
System::Reflection::Assembly.load(assembly).get_types.each do |t|
if t.to_class.implements? "IPersistence"
t.to_class.extend ActsAsActiveRecord
end
end
end
true
endI’m sure there’s lots of other cool uses for the pattern… If anyone actually reads this shit and thinks of some, let me know. :)
I'm in love with IronRuby
I’ve been playing with IronRuby a lot lately, and it’s turning out to be every bit as amazing as I’d hoped it would be. I’m constantly thinking about new stuff to do with it, but one of the easiest low hanging fruits is rewriting our existing unit tests in ruby.
I’ve been putting together a little library of IronRuby specific modules for tests. You’ve got to love being able to do things like this:
class Class
def should_implement(interface)
!to_clr_type.nil? && !to_clr_type.get_interface(interface).nil?
end
end
>>> MyClass.should_implement "ISomething"
=> trueBringing the power of Ruby to .NET is going to be incredible. It feels great clicking both the Ruby and .NET tags on a post. More on this to come…
Javascript Gotchas Are Fun!
And by fun I mean "will make you bang your head against the wall repeatedly."
Explanation here: http://www.jibbering.com/faq/#FAQ4_7