[Ironruby-core] Extending modules defined in a ruby library via C#

Daniele Alessandri suppakilla at gmail.com
Sun Feb 1 04:54:38 EST 2009

On Sun, Feb 1, 2009 at 07:30, Tomas Matousek
<Tomas.Matousek at microsoft.com> wrote:

> That's absolutely true for arbitrary CLR types. Library types
> (those marked by RubyClass attribute) are handled differently
> though. They can be seen like collections of extension methods.
> So it makes sense to extend a Ruby class using library methods.

In fact, I think the described behaviour is perfectly fine for CLR
types and I was sort of expecting this, but not for those types marked
by RubyClass.

> So far we were focused on extending CLR types using library
> types. Extending Ruby classes could be seen as a feature that
> is not implemented yet. Could you file a feature request (bug) on
> RubyForge?

OK, I will file it later. Maybe I will assign a lower priority to this
as I guess it is not really a common scenario and I don't know how
many folks are trying to build extension libs for IronRuby. But still,
IMHO it would be nice to get this fixed before going 1.0.

> A possible workaround for you: define a C# method that takes
> a Ruby class and extends it in manually using methods on
> RubyModule/RubyClass (AddMethod etc.). You would call this
> helper after loading the assembly.

Yeah I was thinking about something along that way, I was just waiting
for confirmations before going on.

FYI I got into this particular case while porting Florian Frank's json
library (see http://json.rubyforge.org/) which provides two variants
for the core bits, namely "extension" (implemented in C) or "pure"
(ruby). Both relies on a few methods, classes and constants previously
defined in the same module in which they are loaded (JSON). When
loading the JSON::Ext::Parser, I was getting everything in JSON and
JSON::Ext erased.

require 'json/common'

module JSON
  module Ext
    # require 'json/ext/parser'
    # require 'json/ext/generator'

    load_assembly 'IronRuby.Libraries.Json', 'IronRuby.Libraries.Json'

    $DEBUG and warn "Using c extension for JSON."
    JSON.parser = Parser
    JSON.generator = Generator

I could also define the Parser and Generator classes into a different
global module and then assign them (e.g. JSON.parser =
IronJson::Ext::Parser), but well there is no fun doing this :-)

Daniele Alessandri

More information about the Ironruby-core mailing list