# RubyModule CreateAnonymousModule - NotImplementedException

**URL:** <https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657>\
**Category:** IronRuby\
**Created:** [September 27, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657 "2008-09-27T14:54:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [September 27, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657/1 "2008-09-27T14:54:16Z")

</div>

Hello,

Just been playing around with IronRuby, however I am hitting an issue  
with RubyModule.CreateAnonymousModule() - basically, it hasn’t been  
implemented yet 🙂

// Ruby constructor:  
public static RubyModule/_!_/  
CreateAnonymousModule([NotNull]BlockParam/_!_/ block, RubyClass/_!_/  
self) {  
// TODO:  
throw new NotImplementedException();  
}

You have implemented another method:  
public static RubyModule/_!_/ CreateAnonymousModule(RubyClass/_!_/ self)  
{  
return self.ExecutionContext.DefineModule(SymbolId.Empty);  
}

But I don’t know how to to implement with relation to the block. Is  
it just not supported yet?

If you could provide me with any help that would be great, its really  
blocking me at the moment.

Thanks  
Ben

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [September 27, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657/2 "2008-09-27T18:09:21Z")

</div>

The documentation says that the block version of the code creates the  
anonymous module and then passes this new module to the block via a  
dynamic  
call to Module#module\_eval.  
The following code is a quick hack (that may work!!) but really we  
should be  
hitting a DynamicSite to invoke the module\_eval.

```
    // Ruby constructor:
    public static object 

```

CreateAnonymousModule([NotNull]BlockParam/_!_/  
block, RubyClass/_!_/ self) {  
RubyModule module =  
self.ExecutionContext.DefineModule(SymbolId.Empty);  
return RubyUtils.EvaluateInModule(module, block);  
}

Disclaimer: I haven’t tried this at all.  
Pete

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [September 28, 2008, 12:10am UTC](https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657/3 "2008-09-28T00:10:51Z")

</div>

Hi Peter,

Thanks for your reply. That is what I tried initially, however you  
need to return a RubyModule object. I tried:

return (RubyModule) RubyUtils.EvaluateInModule(module, block);

but I get the errror:

E:\IronRuby\trunk\src\Microsoft.Scripting.Core\Actions\MatchCaller.Generated.cs:35:in  
`Call3': Unable to cast object of type 'IronRuby.Builtins.Proc' to type 'IronRuby.Builtins.RubyModule'. (System::InvalidCastException) from E:\IronRuby\trunk\src\Microsoft.Scripting.Core\Actions\CallSite.cs:275:in`UpdateAndExecute’  
from  
E:\IronRuby\trunk\src\Microsoft.Scripting.Core\Actions\UpdateDelegates.Generated.cs:45:in  
`Update3' from step.rb:19:in`init\_module’

I tried this implementation:  
RubyModule module = new RubyModule(self);  
module.Name = SymbolId.Empty;  
RubyUtils.EvaluateInModule(module, block);  
return module;

When appears to allow me to progress some more, but now i’m hitting  
LocalJumpError(“unexpected return”); in  
RuntimeFlowControl.BlockReturn, I guess something isn’t correct.

Out of interest, what documentation is available?

Thanks

Ben

On Sat, Sep 27, 2008 at 5:08 PM, Peter Bacon D.

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [September 28, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657/4 "2008-09-28T16:29:57Z")

</div>

Hi Ben,  
Here is a better version that should work, except for the dynamic monkey  
patching aspect. In RubyModule.cs you have to have this method:

```
    // Ruby constructor:
    public static RubyModule/*!*/

```

CreateAnonymousModule([NotNull]BlockParam/_!_/ block, RubyClass/_!_/  
self) {  
RubyModule module =  
self.ExecutionContext.DefineModule(SymbolId.Empty);  
object returnedValue = RubyUtils.EvaluateInModule(module,  
block);  
if ( block.BlockJumped(returnedValue) ) {  
return null;  
}  
return module;  
}

This additional complexity is because MRI normally returns the module  
rather  
than the result of the block, unless the block jumped (i.e. break).  
The following code works the same in MRI and this version of IronRuby…

Module.new  
Module.new {|m| puts m.class}  
Module.new {|m| puts m.class; 123}  
Module.new {|m| puts m.class; break;}  
Module.new {|m| puts m.class; break; 123}

In MRI this prints Module to the console and then returns the anonymous  
Module object unless the block breaks in which case it returns nil.

By the way, this is still not strictly correct yet, since if we monkey  
patch  
module\_eval this version will not call the new version of module\_eval.

Pete

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [September 28, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657/5 "2008-09-28T16:56:54Z")

</div>

And if you want the monkey patching to work then this should help…

In RubyModule.cs you need:

```
    // Ruby constructor:
    public static RubyModule/*!*/

```

CreateAnonymousModule([NotNull]BlockParam/_!_/ block, RubyClass/_!_/  
self) {  
RubyModule module =  
self.ExecutionContext.DefineModule(SymbolId.Empty);  
object returnedValue =  
RubySites.ModuleEval(block.Proc.LocalScope, module, block.Proc);  
if ( block.BlockJumped(returnedValue) ) {  
return null;  
}  
return module;  
}

And in RubySites.cs you need:

```
    private static readonly CallSite<Func<CallSite, CodeContext,

```

RubyModule, Proc, object\>\> ModuleEvalSharedSite =  
CallSite\<Func\<CallSite,  
CodeContext, RubyModule, Proc, object\>\>.Create(  
InstanceCallAction(“module\_eval”,  
RubyCallSignature.WithBlock(0)));

```
    public static object ModuleEval(CodeContext context, RubyModule

```

self, Proc block) {  
return ModuleEvalSharedSite.Target(ModuleEvalSharedSite,  
context, self, block);  
}

There are probably a number of corner cases that I have missed but this  
should work for the majority of uses.

Regards,  
Pete

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [September 28, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657/6 "2008-09-28T15:28:26Z")

</div>

The only documentation I use in these matters is at  
[RDoc Documentation](http://www.ruby-doc.org/core/).  
For example: [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001705).  
Pete

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [September 28, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/rubymodule-createanonymousmodule-notimplementedexception/149657/7 "2008-09-28T18:26:21Z")

</div>

Thanks Peter, I really appreciate it.

On Sun, Sep 28, 2008 at 3:55 PM, Peter Bacon D.
