RE: [Rubydotnet-developer] Introduction
Status: Alpha
Brought to you by:
thomas
From: Ben S. <bsc...@pr...> - 2003-07-18 13:30:53
|
Hi all, I've talked with a couple of you by private mail, but I thought I'd = write a quick introduction to the list as well. As John Pierce = mentioned, he and I have been working on a bridge from Ruby to Dot-Net. = Here is a basic example to show how code written using our bridge looks = - it's the same one I've sent in email to a couple of you. It starts a = Dot-Net machine, creates and manipulates an ArrayList, and uses = Enumerable methods to get a reversed copy. require 'dotnet' dotNet =3D DotNet.startNew list =3D dotNet.ArrayList.new list.add('Hello') list.add('World') puts list.count # -> prints <<2>> reversed =3D list.collect do |each| each.reverse end puts reversed.inspect # -> prints <<["olleH", "dlroW"]>> We automatically convert Strings and Fixnums to Dot-Net Strings and = Int32s, and vice-versa. We do the same with nil and booleans, and plan = to do so for floats and integers of different lengths. I think this = makes for smoother code, although it has the disadvantage of not = allowing calls to methods of the corresponding Dot-Net classes. (Maybe = a way to escape the behavior would be useful.) To create a Dot-Net object, you ask a DotNet instance for the class and = then send "new" to it, like any other Ruby class. You do not have to = use namespaces to qualify the class (one of my favorite ideas of John's = for this program), but may use them if a reference would otherwise be = ambiguous. The bridge will select one of the classes if you do not use = namespaces - which can be confusing. dotNet.ArrayList.new # -> a new ArrayList dotNet.System.Windows.Forms.Label.new # -> would otherwise be ambiguous = with=20 # = System.Reflection.Emit.Label Static methods can also be called using the class object. dotNet.ArrayList.repeat('a', 5) # -> a list containing 'a', 'a', 'a', = 'a', 'a' The class object can be used as a Type object when you pass it as a = parameter, and you can call static methods or constructors on Types. listType =3D dotNet.Type.getType('System.Collections.ArrayList') listType.getType.fullName # -> 'System.RuntimeType' listType.new # -> an ArrayList You can use any Dot-Net libraries with the bridge, including Windows = Forms. Here's an example of event handling for a simple button. dotNet.loadLibrary('System.Windows.Forms') button =3D dotNet.Button.new button.click.add do |sender, args| puts "Button clicked!" end button.performClick # -> prints <<Button clicked!>> As you have probably noticed, we do have to qualify everything through a = DotNet instance. dotNet.ArrayList.new dotNet.Hashtable.new # instead of=20 ArrayList.new Hashtable.new This is very flexible - for example, you could write a test program that = controlled a client and server - but is a bit inconvenient for the = common case where you're just controlling one Dot-Net VM from Ruby. = We've talked about ways to solve this, which I think others have = mentioned as well, like enumerating available types from a single = Dot-Net and introducing them as constants. There are still a lot of loose ends, and things we'd like to do, but we = have successfully used the program to write some small utilities and to = script objects from our production programs. I am planning to put = together some sort of "release" soon with more notes, but would of = course be happy to discuss anything here as well in the meantime. I'll put in a plug too for John's bridge-running webserver - I think = it's pretty neat. Regards, Ben Schroeder |