From: Tim Sutherland Date: 2003-07-31T20:19:07+09:00 Subject: Re: Advocacy: Ruby on/with .net In article <20030730143324.GC9127@student.ei.uni-stuttgart.de>, Mauricio Fern�ndez wrote: > On Wed, Jul 30, 2003 at 11:30:02AM +0900, Tim Sutherland wrote: >> In article <007d01c35570$0d1d54b0$a30aa1c8@bambook>, Rodrigo B. de Oliveira >> wrote: >> > I feel exactly the same way. When do we start coding? :-) >> [...] >> >> There is a mailing list >> http://sourceforge.net/mailarchive/forum.php?forum_id=34312 >> for discussing all things to do with Ruby and .NET. >> >> I have code right now which allows you to do things like >> require 'dotnet' >> >> DotNet.System.Console.WriteLine("Hello DotNet World!") >> >> foo = DotNet.System.Collections.Hashtable.new >> foo['one'] = 1 >> puts(foo['one']) # -> 1 >> >> It also allows C# to call Ruby code. > > This is *very* similar in spirit to what I'm doing w/ Java (through > JNI). If I got it right (I did a quick inspection of your code), you're > linking to the C# runtime, so you have both parts running on the same > process at a time (as opposed to communicating through sockets for > instance). Yes, that is how I am doing it. NB: I'm planning to make the first 'real' release of my rubydotnetproxy library by the upcoming Monday. > BTW, you have achieved about the same (if not more) in < 2Klocs than me > in 8Kloc (although some 4Kloc are auto-generated). > > Did you solve the problem w/ threads (does it happen at all w/ > C# ?) > > In my case, Java threads are spawned as "real" (native) threads, so if > any of them tries to call Ruby code the thing will bomb (Ruby not being > thread safe). No, I did not solve the thread problem. In addition to the problem with dotnet threads, if you are using Ruby threads then a call to a dotnet object can block, preventing other Ruby threads from running. > A few more questions: > * are you doing proper method dispatching on method overload? > This can be tricky if you perform automatic Ruby -> C#/Java type > conversions as there might be several matching methods I only do a few automatic type conversions, e.g. strings. What problems are you thinking of? The dotnet method I am using is Type#InvokeMember which claims to choose the best method. > * do you have metaclasses for static methods or such? Do you think it's > sensible to provide them? (I did that in rjni) I'm not sure what you mean. You can do klass = DotNet.SomeNamespace.SomeClass klass.SomeStaticMethod("Hello") inst = klass.new(1, 2, 3) inst.SomeStaticMethod("World") inst.SomeInstanceMethod(42) > I'll take another look at your code when I have some time but as I know > nothing about .net I don't know if I will understand it. > > And last: is it just me, or C# is insanely easier to interface with than Java? > There's several things in your code that take many more lines when using JNI... I've never used JNI, so I can't say. I think it would have been easier for me if I'd been willing to use Microsoft's "Managed C++" language. This is a language based off C++ which can easily call dotnet code, C code and be called by C code. Unfortunately Mono is not planning to implement this language so I wrote half of my library in C# instead. It is easy to call C code from C#, however it is not so easy to call C# code from C. My solution is to have a small (~10 lines) library written in Managed C++ which causes the C# library to call a C function in my library, passing in a bunch of function pointers. The C code can then call these functions to access dotnet features. Ben Schroeder and John R. Pierce have written a similar library which uses sockets. It implements an interesting feature (not yet available in my library) which is to turn Ruby blocks into C# Delegates (these are like function pointers.) Ben offers the following example: dotNet.loadLibrary('System.Windows.Forms') button = dotNet.Button.new button.click.add do |sender, args| puts "Button clicked!" end button.performClick # -> prints <