# Initializing external Ruby libraries

**URL:** <https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078>\
**Category:** IronRuby\
**Created:** [February 14, 2008, 1:00am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078 "2008-02-14T01:00:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 1:00am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/1 "2008-02-14T01:00:00Z")

</div>

For Ruby libraries such as sockets, openssl etc (ie not built-in classes  
or modules), I assume we will implement/port them using say C# to  
produce a managed dll (eg socket.dll). Client programs will then require  
‘socket’, and IronRuby will load socket.dll rather than socket.so

I note IronRuby already has the ability to load managed dlls such as  
mscorlib - but that appears to simply load the assembly and then  
presumably uses .NET reflection to expose the .NET classes as Ruby  
classes. But for managed dll such as socket.dll that has been  
specifically engineered as an IronRuby extension dll, I image the  
loading process would be a little different? CRuby, for example exposes  
an Init\_Foo function which explicitly calls define\_class, define\_method  
etc to explicitly register each of the Ruby classes implemented by that  
library (equivalent to what IronRuby does in  
LibrariesInitializer.LoadModules).

Will there be a similar convention used by IronRuby? Possibly in static  
class constructors?

Cheers, Wayne.

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 2:01am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/2 "2008-02-14T02:01:35Z")

</div>

I believe the library side of this mechanism is already in-place.

Take a look at the  
\trunk\src\IronRuby.Libraries\Initializer.Generated.cs  
file which is automatically generated by the  
\trunk\src\IronRuby.Libraries\GenerateInitializers.cmd. The cmd file  
reflects on the compiled library file, reading the RubyClass,  
RubyMethod,  
etc. attributes and generates the initialize code you describe below.  
You  
can see this in action inside \trunk\src\ironruby\Runtime\Loader.cs :  
Ruby.Runtime.Loader.LoadStandardRubyLibraries().

What appears to be missing is the hosting side of things when you  
“require”  
one of these assemblies. There doesn’t seem to be any point in the  
assembly  
loading process that checks for a LibraryInitializer class and runs the  
LoadModules method.

Is that what you meant?  
Pete

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Wayne K.  
Sent: Wednesday,13 February 13, 2008 23:59  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] Initializing external Ruby libraries

For Ruby libraries such as sockets, openssl etc (ie not built-in classes  
or  
modules), I assume we will implement/port them using say C# to produce a  
managed dll (eg socket.dll). Client programs will then require ‘socket’,  
and  
IronRuby will load socket.dll rather than socket.so

I note IronRuby already has the ability to load managed dlls such as  
mscorlib - but that appears to simply load the assembly and then  
presumably  
uses .NET reflection to expose the .NET classes as Ruby classes. But for  
managed dll such as socket.dll that has been specifically engineered as  
an  
IronRuby extension dll, I image the loading process would be a little  
different? CRuby, for example exposes an Init\_Foo function which  
explicitly  
calls define\_class, define\_method etc to explicitly register each of the  
Ruby classes implemented by that library (equivalent to what IronRuby  
does  
in LibrariesInitializer.LoadModules).

Will there be a similar convention used by IronRuby? Possibly in static  
class constructors?

Cheers, Wayne.

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 2:14am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/3 "2008-02-14T02:14:28Z")

</div>

Michael L.:

> What do you see would be the advantage of something like this as  
> opposed to the current method of loading .NET assemblies? I mean you  
> could use something like [OpenSSL.NET](http://OpenSSL.NET)  
> ([http://openssl-net.sourceforge.net/](http://openssl-net.sourceforge.net/)) today to implement ssl in  
> IronRuby for example

The main reason for implementing the libraries in C# would be for better  
performance. But if it’s just a thin wrapper on top of some .NET piece,  
there’s no reason you couldn’t implement it in Ruby code instead of C#

- John

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 2:46am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/4 "2008-02-14T02:46:05Z")

</div>

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Peter Bacon  
Darwin  
Sent: Thursday, 14 February 2008 11:01 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Initializing external Ruby libraries

I believe the library side of this mechanism is already in-place.

Take a look at the  
\trunk\src\IronRuby.Libraries\Initializer.Generated.cs file which is  
automatically generated by the  
\trunk\src\IronRuby.Libraries\GenerateInitializers.cmd. The cmd file  
reflects on the compiled library file, reading the RubyClass,  
RubyMethod, etc. attributes and generates the initialize code you  
describe below. You can see this in action inside  
\trunk\src\ironruby\Runtime\Loader.cs :  
Ruby.Runtime.Loader.LoadStandardRubyLibraries().

What appears to be missing is the hosting side of things when you  
“require” one of these assemblies. There doesn’t seem to be any point  
in the assembly loading process that checks for a LibraryInitializer  
class and runs the LoadModules method.

Is that what you meant?  
Yes, exactly.

Cheers, Wayne.

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 2:54am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/5 "2008-02-14T02:54:17Z")

</div>

Yes, my only point is that the way it works today a .NET class becomes  
a Ruby class and a .NET method becomes a Ruby method. Nothing is  
stopping one from writing a .NET assembly so that it “feels” like Ruby  
when it’s compiled.

That said, I can see the advantage of being able to use the RubyClass  
and RubyMethod attributes outside of that…

Is the performance between this and utilizing an Initializer that great?

On Feb 13, 2008 8:13 PM, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - John
> 
> * * *
> 
> Ironruby-core mailing list  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

–  
Michael L.  
[Polymath Programmer]  
[http://michaeldotnet.blogspot.com](http://michaeldotnet.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 3:00am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/6 "2008-02-14T03:00:09Z")

</div>

> I mean you could use something like [OpenSSL.NET](http://OpenSSL.NET)  
> ([http://openssl-net.sourceforge.net/](http://openssl-net.sourceforge.net/)) today to implement ssl  
> in IronRuby for example

It’s not just about getting access to some implementation of OpenSSL, it  
about providing a set of Ruby classes and methods that exactly match  
those exposed by existing Ruby extension libraries used by CRuby. By  
explicitly calling define\_class, define\_method etc, you have better  
control over how your C# code is presented to the Ruby world. You can,  
for example, specify a Ruby base class that may not exist as a static  
.NET class. I’m not sure if this is actually an issue in practice. But  
then if there’s no need to call define\_class and define\_method  
explicitly for external libraries, then why do it for the built-in  
classes? Why not simply load an RubyExternalLibrary.dll? Is it for  
performance, for preciseness of interface or both?

Cheers, Wayne.

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 2:03am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/7 "2008-02-14T02:03:55Z")

</div>

What do you see would be the advantage of something like this as  
opposed to the current method of loading .NET assemblies? I mean you  
could use something like [OpenSSL.NET](http://OpenSSL.NET)  
([http://openssl-net.sourceforge.net/](http://openssl-net.sourceforge.net/)) today to implement ssl in  
IronRuby for example

I believe a future goal is to actually support loading unmanaged  
extensions (this is based strictly on comments in the code, noone’s  
actually said this as far as I know).

On Feb 13, 2008 6:58 PM, Wayne K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> managed dll such as socket.dll that has been specifically engineered as an
> 
> * * *
> 
> Ironruby-core mailing list  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

–  
Michael L.  
[Polymath Programmer]  
[http://michaeldotnet.blogspot.com](http://michaeldotnet.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 3:01am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/8 "2008-02-14T03:01:19Z")

</div>

Michael L.:

> 

Sorry, I misunderstood. I thought you were suggesting writing the  
library as a .rb file & using .NET interop features to call into the  
.NET code.

Yeah, you couldn’t just write it in C# because we need the initializer  
to give proper Ruby method names &  
public/protected/private/instance/singleton metadata. It’s also a  
performance win because we don’t need to use reflection to pull out the  
metadata at runtime.

- John

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [February 14, 2008, 3:18am UTC](https://www.ruby-forum.com/t/initializing-external-ruby-libraries/129078/9 "2008-02-14T03:18:55Z")

</div>

I assume then we will be using the RubyLibrary, RubyClass, RubyMethod  
attributes, et al to implement that then?

On Feb 13, 2008 9:00 PM, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Is the performance between this and utilizing an Initializer that  
> > Ironruby-core mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

–  
Michael L.  
[Polymath Programmer]  
[http://michaeldotnet.blogspot.com](http://michaeldotnet.blogspot.com)
