IronRuby C# Extensions

Hi,

I’m trying to create an IronRuby C# extensions and I just can’t get it
to
work. Is there anything special I should do?

This is what I do:

  1. Create a class library (.Net 2.0)
  2. Create a class and use the RubyModule attribute on it with a name.
    Then I
    set some classes and consts inside and decorate them with RubyClass and
    RubyConstant attributes.
  3. I compile that.
  4. I load ir.exe and use load_assembly in order to load the assembly
    with
    the extension.
    The problem is that the class library is loaded without paying attention
    to
    the attributes and I have to use the namespace and CLR name. What am I
    missing?

Thanks!
Shay.

I found out I needed an initializer…

I have a question - why do we need initializers if we define everything
on the attributes?

Thanks,
Shay.

Basically to avoid reflection when loading the classes and modules.

Tomas

Just require an assembly should work just as well I guess.
When would you need to use the RubyClass etc attributes and the
initializer?
What is the added value of those?

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

Is it possible to consider allowing to load extensions without the need
of a
library initializer? (another argument in the load_assembly method for
example)
Currently, the library initializer makes the whole thing much more
complicated…

Thanks,
Shay.

On Sun, Oct 11, 2009 at 6:27 PM, Tomas M. <

2 samples I can think of -

  1. Porting Ruby native extensions
  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation

Shay.

But wouldn’t you be able to accomplish these scenarios just by requiring
a
C# built dll?

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

By the way, if we’re at the subject… Ivan does have a point there, but
why
were the standard libraries written this way?

Thanks,
Shay.

Right, but this way you have more control on the “translation” of your
Dll
to IronRuby.

Shay.

Ideally Reflection would be fast and we wouldn’t need any workarounds to
load built-ins fast. But that’s not the case, so we use initializers.

As for the scenarios you mentioned:

  1. Porting Ruby native extensions
    Why not to write them in Ruby with calls to .NET Framework assemblies?
    Is there anything that makes this difficult?

  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation
    Couldn’t you write the parts whose perf matter in C# code that doesn’t
    have all the Ruby bells and whistles and keep your library written in
    Ruby?

Tomas

From: [email protected]
[mailto:[email protected]] On Behalf Of Shay F.
Sent: Sunday, October 11, 2009 10:26 AM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

By the way, if we’re at the subject… Ivan does have a point there, but
why were the standard libraries written this way?

Thanks,
Shay.
On Sun, Oct 11, 2009 at 7:19 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
Right, but this way you have more control on the “translation” of your
Dll to IronRuby.

Shay.

On Sun, Oct 11, 2009 at 7:14 PM, Ivan Porto C.
<[email protected]mailto:[email protected]> wrote:
But wouldn’t you be able to accomplish these scenarios just by requiring
a C# built dll?


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Sun, Oct 11, 2009 at 7:03 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
2 samples I can think of -

  1. Porting Ruby native extensions
  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation

Shay.

On Sun, Oct 11, 2009 at 6:57 PM, Ivan Porto C.
<[email protected]mailto:[email protected]> wrote:
Just require an assembly should work just as well I guess.

When would you need to use the RubyClass etc attributes and the
initializer? What is the added value of those?

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Sun, Oct 11, 2009 at 6:51 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
Is it possible to consider allowing to load extensions without the need
of a library initializer? (another argument in the load_assembly method
for example)
Currently, the library initializer makes the whole thing much more
complicated…

Thanks,
Shay.

On Sun, Oct 11, 2009 at 6:27 PM, Tomas M.
<[email protected]mailto:[email protected]>
wrote:
Basically to avoid reflection when loading the classes and modules.

Tomas

Tomas, assuming you wanted your library to be C# code only for whatever
reason, wouldn’t you need to use RubyModuleAttribute etc for various
reasons (other than performance) to make the library look really
Ruby-friendly and also be multi-ScriptRuntime-aware? For example, if you
wanted a class constant, you couldn’t just declare a C# “const” variable
as that would be appdomain-wide and also read-only. So you have to tag
it with “RubyContantAttribute”, right?

From: [email protected]
[mailto:[email protected]] On Behalf Of Tomas M.
Sent: Sunday, October 11, 2009 11:48 AM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

Ideally Reflection would be fast and we wouldn’t need any workarounds to
load built-ins fast. But that’s not the case, so we use initializers.

As for the scenarios you mentioned:

  1. Porting Ruby native extensions
    Why not to write them in Ruby with calls to .NET Framework assemblies?
    Is there anything that makes this difficult?

  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation
    Couldn’t you write the parts whose perf matter in C# code that doesn’t
    have all the Ruby bells and whistles and keep your library written in
    Ruby?

Tomas

From: [email protected]
[mailto:[email protected]] On Behalf Of Shay F.
Sent: Sunday, October 11, 2009 10:26 AM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

By the way, if we’re at the subject… Ivan does have a point there, but
why were the standard libraries written this way?

Thanks,
Shay.
On Sun, Oct 11, 2009 at 7:19 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
Right, but this way you have more control on the “translation” of your
Dll to IronRuby.

Shay.

On Sun, Oct 11, 2009 at 7:14 PM, Ivan Porto C.
<[email protected]mailto:removed_e[email protected]> wrote:
But wouldn’t you be able to accomplish these scenarios just by requiring
a C# built dll?


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Sun, Oct 11, 2009 at 7:03 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
2 samples I can think of -

  1. Porting Ruby native extensions
  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation

Shay.

On Sun, Oct 11, 2009 at 6:57 PM, Ivan Porto C.
<[email protected]mailto:[email protected]> wrote:
Just require an assembly should work just as well I guess.

When would you need to use the RubyClass etc attributes and the
initializer? What is the added value of those?

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Sun, Oct 11, 2009 at 6:51 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
Is it possible to consider allowing to load extensions without the need
of a library initializer? (another argument in the load_assembly method
for example)
Currently, the library initializer makes the whole thing much more
complicated…

Thanks,
Shay.

On Sun, Oct 11, 2009 at 6:27 PM, Tomas M.
<[email protected]mailto:[email protected]>
wrote:
Basically to avoid reflection when loading the classes and modules.

Tomas

To clarify things a bit more, the real difference between the two ways
is performance and cleanness of the .NET code. My guidance would be to
initially build a ruby front-end API, and call into .NET when needed;
the cleanest solution IMO but depends on slow reflection for calling
into .NET code. If you find that the .NET integration is too slow for
that particular library, then you can move your code over to the
generated-initializers model that IronRuby’s libraries use.

~js


From: [email protected]
[[email protected]] on behalf of Shri B.
[[email protected]]
Sent: Sunday, October 11, 2009 12:44 PM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

Tomas, assuming you wanted your library to be C# code only for whatever
reason, wouldn’t you need to use RubyModuleAttribute etc for various
reasons (other than performance) to make the library look really
Ruby-friendly and also be multi-ScriptRuntime-aware? For example, if you
wanted a class constant, you couldn’t just declare a C# “const” variable
as that would be appdomain-wide and also read-only. So you have to tag
it with “RubyContantAttribute”, right?

From: [email protected]
[mailto:[email protected]] On Behalf Of Tomas M.
Sent: Sunday, October 11, 2009 11:48 AM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

Ideally Reflection would be fast and we wouldn’t need any workarounds to
load built-ins fast. But that’s not the case, so we use initializers.

As for the scenarios you mentioned:

  1. Porting Ruby native extensions
    Why not to write them in Ruby with calls to .NET Framework assemblies?
    Is there anything that makes this difficult?

  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation
    Couldn’t you write the parts whose perf matter in C# code that doesn’t
    have all the Ruby bells and whistles and keep your library written in
    Ruby?

Tomas

From: [email protected]
[mailto:[email protected]] On Behalf Of Shay F.
Sent: Sunday, October 11, 2009 10:26 AM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

By the way, if we’re at the subject… Ivan does have a point there, but
why were the standard libraries written this way?

Thanks,
Shay.
On Sun, Oct 11, 2009 at 7:19 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
Right, but this way you have more control on the “translation” of your
Dll to IronRuby.

Shay.

On Sun, Oct 11, 2009 at 7:14 PM, Ivan Porto C.
<[email protected]mailto:[email protected]> wrote:
But wouldn’t you be able to accomplish these scenarios just by requiring
a C# built dll?


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Sun, Oct 11, 2009 at 7:03 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
2 samples I can think of -

  1. Porting Ruby native extensions
  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation

Shay.

On Sun, Oct 11, 2009 at 6:57 PM, Ivan Porto C.
<[email protected]mailto:[email protected]> wrote:
Just require an assembly should work just as well I guess.

When would you need to use the RubyClass etc attributes and the
initializer? What is the added value of those?

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Sun, Oct 11, 2009 at 6:51 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
Is it possible to consider allowing to load extensions without the need
of a library initializer? (another argument in the load_assembly method
for example)
Currently, the library initializer makes the whole thing much more
complicated…

Thanks,
Shay.

On Sun, Oct 11, 2009 at 6:27 PM, Tomas M.
<[email protected]mailto:[email protected]>
wrote:
Basically to avoid reflection when loading the classes and modules.

Tomas

Thanks for the replies guys.

By the way, if the initializer is required, why are the attributes
needed
too? (or they aren’t?)

Thanks,
Shay.

On Sun, Oct 11, 2009 at 10:06 PM, Jimmy S. <

for the initializer to know which classes to use when generating and
what to
generate I guess

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

Right, you might need method names containing “!” and “?” characters or
operator method names like “===” etc. The constants are also easier to
setup in a library that’s using the initializers. The initializers are
no magic though. The generated initializer for a given library is
invoked by load_assembly method in file.rb that you require to load the
library. For example, enumerator.rb:

load_assembly ‘IronRuby.Libraries’,
‘IronRuby.StandardLibrary.Enumerator’

RubyMethod/RubyClass/RubyConstant/… attributes declare what the
generated initializers should do. They are not used at runtime. You can
write your custom code that builds Ruby classes and registers them to
the runtime using APIs on RubyModule/RubyContext. You could then invoke
the code from your_lib.rb file that you require to load your library.

Tomas

From: [email protected]
[mailto:[email protected]] On Behalf Of Shri B.
Sent: Sunday, October 11, 2009 12:44 PM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

Tomas, assuming you wanted your library to be C# code only for whatever
reason, wouldn’t you need to use RubyModuleAttribute etc for various
reasons (other than performance) to make the library look really
Ruby-friendly and also be multi-ScriptRuntime-aware? For example, if you
wanted a class constant, you couldn’t just declare a C# “const” variable
as that would be appdomain-wide and also read-only. So you have to tag
it with “RubyContantAttribute”, right?

From: [email protected]
[mailto:[email protected]] On Behalf Of Tomas M.
Sent: Sunday, October 11, 2009 11:48 AM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

Ideally Reflection would be fast and we wouldn’t need any workarounds to
load built-ins fast. But that’s not the case, so we use initializers.

As for the scenarios you mentioned:

  1. Porting Ruby native extensions
    Why not to write them in Ruby with calls to .NET Framework assemblies?
    Is there anything that makes this difficult?

  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation
    Couldn’t you write the parts whose perf matter in C# code that doesn’t
    have all the Ruby bells and whistles and keep your library written in
    Ruby?

Tomas

From: [email protected]
[mailto:[email protected]] On Behalf Of Shay F.
Sent: Sunday, October 11, 2009 10:26 AM
To: [email protected]
Subject: Re: [Ironruby-core] IronRuby C# Extensions

By the way, if we’re at the subject… Ivan does have a point there, but
why were the standard libraries written this way?

Thanks,
Shay.
On Sun, Oct 11, 2009 at 7:19 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
Right, but this way you have more control on the “translation” of your
Dll to IronRuby.

Shay.

On Sun, Oct 11, 2009 at 7:14 PM, Ivan Porto C.
<[email protected]mailto:[email protected]> wrote:
But wouldn’t you be able to accomplish these scenarios just by requiring
a C# built dll?


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)
On Sun, Oct 11, 2009 at 7:03 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
2 samples I can think of -

  1. Porting Ruby native extensions
  2. Create an IronRuby library in C# in order to improve performance of a
    certain operation

Shay.

On Sun, Oct 11, 2009 at 6:57 PM, Ivan Porto C.
<[email protected]mailto:[email protected]> wrote:
Just require an assembly should work just as well I guess.

When would you need to use the RubyClass etc attributes and the
initializer? What is the added value of those?

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Sun, Oct 11, 2009 at 6:51 PM, Shay F.
<[email protected]mailto:[email protected]> wrote:
Is it possible to consider allowing to load extensions without the need
of a library initializer? (another argument in the load_assembly method
for example)
Currently, the library initializer makes the whole thing much more
complicated…

Thanks,
Shay.

On Sun, Oct 11, 2009 at 6:27 PM, Tomas M.
<[email protected]mailto:[email protected]>
wrote:
Basically to avoid reflection when loading the classes and modules.

Tomas

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs