# Should Kernel.require accept Assembly instances?

**URL:** <https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850>\
**Category:** IronRuby\
**Created:** [August 7, 2010, 12:09am UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850 "2010-08-07T00:09:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 7, 2010, 12:09am UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/1 "2010-08-07T00:09:45Z")

</div>

What would you all think of having the ability to require a given  
Assembly?  
I think this could be useful when compiling code in memory, in which  
case  
there isn’t a path to give Kernel.require.

If this is something we could all use, I’ll open a ticket for it.

-Charles

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 7, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/2 "2010-08-07T12:37:07Z")

</div>

What’s the advantage to extending require?

Presumably you’re currently using the .NET Assembly.Load or  
Assembly.LoadFrom methods to do this? (And if you’re compiling code in  
memory, you’ll certainly be making heavy use of the .NET reflection  
API’s already anyway)

Require is a standard part of core ruby, and is meant to take paths.  
While it’s obvious to overload it to accept paths to dll’s as well as rb  
files, overloading it to take non-path things (such as .NET assembly  
objects) seems like it’s diverging a bit too far away from it’s normal  
(ie: MRI ruby) use, and more into the realms of specific .NET  
extensions…

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 9, 2010, 7:01pm UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/3 "2010-08-09T19:01:40Z")

</div>

Those are valid points. Perhaps #load\_assembly could accept an  
assembly reference.

Sent from my iPhone

On Aug 7, 2010, at 5:16 PM, Orion E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 9, 2010, 11:28pm UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/4 "2010-08-09T23:28:38Z")

</div>

I’m looking through the MSDN docs for assembly loading, and it seems as  
though you can either load an assembly from a path, or from a byte  
array.  
Both of these methods return an Assembly object.

There doesn’t appear to be any other way to actually get an Assembly  
object  
other than by loading it, as the constructor is protected (assembly is  
abstract), and the only classes that I can see in the framework that  
derive  
from it are the internal RuntimeAssembly class (which is used for  
everything  
pretty much), and System.Reflection.Emit.AssemblyBuilder.

As far as I can infer, the only way to actual get an assembly object is  
to  
load the assembly, so if you’re asking how you can load an assembly  
given an  
Assembly object… it’s already loaded.

Am I missing something?

On Tue, Aug 10, 2010 at 4:49 AM, Charles S. \<

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 10, 2010, 8:16pm UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/5 "2010-08-10T20:16:46Z")

</div>

Orion,

Yes, I can use Assembly.LoadFrom to load an assembly from a path (and I  
am  
doing that), but that’s not _all_ I want to do. I think the easiest way  
to  
communicate my intentions is to ask you the following question:

Q: What happens when I call Kernel.load\_assembly in IronRuby, provided I  
pass in some assembly name?

A: Modules are created that reflect the types and namespaces within the  
assembly (::System::InteropServices, ::System::Reflection::Assembly,  
etc).

That’s the effect I want. If I just use Assembly.LoadFrom, IronRuby  
will  
not treat that the same way as Kernel.load\_assembly, nor should it.

Do you see where I’m going with this?

I thought I had found a way to hack around this by getting to the  
current  
context with this little hack:

# ::Object is an instance of RubyClass, which holds a reference to the

RubyContext within which it was created.

# However, IronRuby hides the Context property, so you can’t do

Object.context, Kernel.context, etc (which is a good thing).

# But, with a little reflection (and because I know Context really is

there), I can do the following:  
context = Object.GetType.get\_members.find { |m| m.name == ‘Context’  
}.get\_value(Object, nil)

And then I figured I could do something like this:  
context.loader.load\_assembly(…)

… but the overload I need is marked private (the one that is public  
expects a string containing the assembly’s name, as opposed to path). I  
suppose I could use reflection again, but it wouldn’t work without full  
trust. It was a cool idea, nonetheless.

-Charles

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 10, 2010, 10:05pm UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/6 "2010-08-10T22:05:06Z")

</div>

I’ll send you pull request on GitHub, if that will work.

-Charles

On Tue, Aug 10, 2010 at 1:32 PM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 10, 2010, 8:44pm UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/7 "2010-08-10T20:44:17Z")

</div>

I think it makes sense to add an overload for load\_assembly that takes  
Assembly object instead of name. Charles, feel free to submit a patch or  
file a bug to trace the feature request and I’ll get to it soon.

Tomas

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Charles  
Strahan  
Sent: Tuesday, August 10, 2010 11:04 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Should Kernel.require accept Assembly  
instances?

Orion,

Yes, I can use Assembly.LoadFrom to load an assembly from a path (and I  
am doing that), but that’s not _all_ I want to do. I think the easiest  
way to communicate my intentions is to ask you the following question:

Q: What happens when I call Kernel.load\_assembly in IronRuby, provided I  
pass in some assembly name?

A: Modules are created that reflect the types and namespaces within the  
assembly (::System::InteropServices, ::System::Reflection::Assembly,  
etc).

That’s the effect I want. If I just use Assembly.LoadFrom, IronRuby  
will not treat that the same way as Kernel.load\_assembly, nor should it.

Do you see where I’m going with this?

I thought I had found a way to hack around this by getting to the  
current context with this little hack:

# ::Object is an instance of RubyClass, which holds a reference to the

RubyContext within which it was created.

# However, IronRuby hides the Context property, so you can’t do

Object.context, Kernel.context, etc (which is a good thing).

# But, with a little reflection (and because I know Context really is

there), I can do the following:  
context = Object.GetType.get\_members.find { |m| m.name[http://m.name](http://m.name) ==  
‘Context’ }.get\_value(Object, nil)

And then I figured I could do something like this:  
context.loader.load\_assembly(…)

… but the overload I need is marked private (the one that is public  
expects a string containing the assembly’s name, as opposed to path). I  
suppose I could use reflection again, but it wouldn’t work without full  
trust. It was a cool idea, nonetheless.

-Charles

On Mon, Aug 9, 2010 at 3:45 PM, Orion E.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
I’m looking through the MSDN docs for assembly loading, and it seems as  
though you can either load an assembly from a path, or from a byte  
array. Both of these methods return an Assembly object.

There doesn’t appear to be any other way to actually get an Assembly  
object other than by loading it, as the constructor is protected  
(assembly is abstract), and the only classes that I can see in the  
framework that derive from it are the internal RuntimeAssembly class  
(which is used for everything pretty much), and  
System.Reflection.Emit.AssemblyBuilder.

As far as I can infer, the only way to actual get an assembly object is  
to load the assembly, so if you’re asking how you can load an assembly  
given an Assembly object… it’s already loaded.

Am I missing something?

On Tue, Aug 10, 2010 at 4:49 AM, Charles S.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

Those are valid points. Perhaps #load\_assembly could accept an assembly  
reference.

Sent from my iPhone

On Aug 7, 2010, at 5:16 PM, Orion E.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
What’s the advantage to extending require?

Presumably you’re currently using the .NET Assembly.Load or  
Assembly.LoadFrom methods to do this? (And if you’re compiling code in  
memory, you’ll certainly be making heavy use of the .NET reflection  
API’s already anyway)

Require is a standard part of core ruby, and is meant to take paths.  
While it’s obvious to overload it to accept paths to dll’s as well as rb  
files, overloading it to take non-path things (such as .NET assembly  
objects) seems like it’s diverging a bit too far away from it’s normal  
(ie: MRI ruby) use, and more into the realms of specific .NET  
extensions…

On 7/08/2010, at 10:08 AM, Charles S. wrote:  
What would you all think of having the ability to require a given  
Assembly? I think this could be useful when compiling code in memory,  
in which case there isn’t a path to give Kernel.require.

If this is something we could all use, I’ll open a ticket for it.

-Charles

* * *

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

* * *

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

* * *

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

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 10, 2010, 10:14pm UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/8 "2010-08-10T22:14:29Z")

</div>

Oh, I almost forgot; thanks for being so awesome, Tomas :). Ruby is an  
absolute joy to program in, and having IronRuby means I don’t have to  
choose  
between .NET and Ruby - I get the best of both worlds. None of that  
would  
have been possible without your contributions and dedication to the  
project.  
In spite of Microsoft’s stance on the future of IronRuby, I hope we can  
carry it forward as a stable, reliable implementation.

Thanks,  
-Charles

On Tue, Aug 10, 2010 at 1:32 PM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 11, 2010, 12:58am UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/9 "2010-08-11T00:58:22Z")

</div>

Yep! I could have sworn my initial proposition had “an instance of type  
Assembly” somewhere in there. Not the clearest communication on my  
part.

-Charles

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [August 11, 2010, 12:32am UTC](https://www.ruby-forum.com/t/should-kernel-require-accept-assembly-instances/192850/10 "2010-08-11T00:32:37Z")

</div>

Ahh, so you’re really after something that “brings in” an already loaded  
assembly into IronRuby. Makes perfect sense now.

And thanks again to Tomas 🙂

On Wed, Aug 11, 2010 at 7:54 AM, Charles S. \<
