# Hosting... executing function dynamically from C#

**URL:** <https://www.ruby-forum.com/t/hosting-executing-function-dynamically-from-c/131680>\
**Category:** IronRuby\
**Created:** [March 12, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/hosting-executing-function-dynamically-from-c/131680 "2008-03-12T13:44:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![devtheo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@devtheo](https://www.ruby-forum.com/u/devtheo)\
**Post date:** [March 12, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/hosting-executing-function-dynamically-from-c/131680/1 "2008-03-12T13:44:05Z")

</div>

Ok,

I’m trying to do something interesting with the DLR (I emailed John  
before finding this forum… sorry, John). My project is here →   
[http://www.codeplex.com/dlrscript](http://www.codeplex.com/dlrscript)

What I’m doing is hosting the DLR within a Silverlight 2.0 beta 1 app.  
My app scans the html dom for Script tags that target DLR languages  
effectively adding a new client scripting language to the browser. I’m  
embedding a couple objects as global vars into the script environment.  
One of these objects provides a mechanism for creating and event handler  
which is used in conjunction with my other object (document which  
provides getElementById). The app itself is written in C#, and my test  
page works perfectly for DLR Python (aka IronPython) and DLR JScript  
(AKA managed JavaScript). For DLR Ruby (AKA IronRuby), the globals are  
working, and the wiring up of the event works. The problem has to do  
with how I’m calling the Ruby function when the event fires (it fires a  
C# anonymous function which calls the Ruby function).

The way I’m calling the function is via an instance of the Ruby  
ScriptEngine’s Execute function using a single statement (source  
kind)… the code looks something like this:  
// This is actually a function  
String method = “someFunction”; /// this is actually a param  
String codeID = “rby\_command”; // this is also a param  
SourceCodeKind kind = SourceCodeKind.SingleStatement; //  
param  
// SourceCodeKind is actually a variable… first time through this is  
// “Statements”  
// This is the actual function beginning  
Exception result = null;  
ErrorSink sink = new ErrorSink();  
try  
{  
CompilerOptions copts =  
engine.GetDefaultCompilerOptions();

```
            SourceUnit scu =

```

engine.CreateScriptSourceFromString(text, scriptid + “\_” +  
this.shortName, kind);

```
            CompilerContext cc = new CompilerContext(scu, copts,

```

sink);  
ICompiledCode icc = engine.Compile(scu, copts, sink);

```
            if (sink.AnyError) return new Exception("Syntax Error :"

```

- sink.ToString());

iteration through  
{  
scope = engine.Runtime.CreateScope();  
}

```
            engine.Execute(scope, scu);
            return null;
        }
        catch (Exception ex)
        {
            result = ex;
        }
        return result;

```

I would expect that as long as I keep the same scope then the  
environment should be able to locate the local variable/function I’m  
trying to call. BTW, I’ve tried interactive, and expression, as well.  
None of them work. So what am I missing? My objects are definitely  
working.

BTW, the code in the latest released project has changed. Ruby doesn’t  
work there (it is working perfectly except for this one item), so if I  
need to I will upload a zip of the project here.

Also, thank you Michael of [Michael.net](http://Michael.net)… you’re logo DSL was very  
helpful for getting me this far with Ruby.

Jay Kimble  
[http://www.theruntime.com/blogs/jaykimble](http://www.theruntime.com/blogs/jaykimble)

---

<div class="post-metadata">

**Author:** ![devtheo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@devtheo](https://www.ruby-forum.com/u/devtheo)\
**Post date:** [March 12, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/hosting-executing-function-dynamically-from-c/131680/2 "2008-03-12T18:46:21Z")

</div>

Could you simplify the example of what you’re trying to do?

Do you want to execute:  
“def foo  
end”

and then  
“foo”

?

What is the source code (“text” variable)?

Tomas

---

<div class="post-metadata">

**Author:** ![devtheo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@devtheo](https://www.ruby-forum.com/u/devtheo)\
**Post date:** [March 12, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/hosting-executing-function-dynamically-from-c/131680/3 "2008-03-12T18:54:21Z")

</div>

Ack! I messed that up. Text is the script to run (I mislabelled it  
above as methodName)… here’s a little more detail:

I have already executed a script that looks like this

* * *

def myFunction()

# do a bunch of stuff

end

## #call code to wire up a button click pushing the function name

When the button is clicked my anonymous C# code attempts to call the  
function passed in (in this case, it would be “myFunction” which would  
be the value of the Text variable in the function above).

What comes back is an error saying that the variable can’t be found.

---

<div class="post-metadata">

**Author:** ![devtheo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@devtheo](https://www.ruby-forum.com/u/devtheo)\
**Post date:** [March 13, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/hosting-executing-function-dynamically-from-c/131680/4 "2008-03-13T14:23:16Z")

</div>

Alright I found the problem and it appears to be a problem between the  
chair and the keyboard… Sorry about that.

I fat fingered my function name, and as soon as I discovered that it  
made sense that Iron Ruby was telling me that it couldn’t find that  
function (because it couldn’t).

Jay
