Using boo as an embedded scripting application

fallenrogue was twittering about IronRuby and Ruby.NET and I just don’t get the .NET infatuation with Ruby. IMO boo is better for most use cases, especially green field development.

Today there was an interesting conversation in #boo on using boo as an embedded scripting application. I don’t think I can summarize how to do this much better than the IRC conversation.

13:47   Avictus| Hello everyone.  Boo newb here.
13:47   Avictus| I have a question that I was hoping someone in here could
                 answer, or at least point me in the right direction.
13:48     Griep| What’s up?
13:48   Avictus| I’m planning on using Boo as an embedded scripting language in
                 a C# app that I’m developing.  I’m trying to figure out how to
                 expose methods and properties of my C# app to the Boo scripts
                 that the app will run.
13:50   Avictus| There are examples on how to compile Boo scripts from a C#
                 app, and how to call methods that are in the Boo script from
                 the C# app.  But I need to be able to call C# methods from the
                 Boo script.
13:50   Avictus| Is it possible to do this with Boo?
13:50     Griep| It is.  I am personally unfamiliar with exactly what would be
                 needed, but let me search our archives and see what I can
                 dredge up.
13:51   Avictus| Thank you very much.  I was looking over the website, but I
                 couldn’t find anything.  Your help is greatly appreciated.
13:55     Griep| So, what you’ll likely want to do is set up Boo as an
                 interpreter.  You can do this in C# by creating a new
                 interpreter: “InteractiveInterpreter interpreter = new
                 Boo.Lang.Interpreter.InteractiveInterpreter()”
13:55     Griep| Then, you can use “interpreter.Eval(scriptString);”
13:56     Griep| You can use interpreter.load(
13:56     Griep| to load in external references, such as to a library.
13:58     Griep| The main library you’ll probably want to look into is
                 Boo.Lang.Interpreter.
13:59   Avictus| So, if I had a method named myObject.GetSomeData() in my C#
                 app, I could call that from within a Boo script using
                 Boo.Lang.Interpreter?
14:02     Griep| Yes.  You can add references to the interpreter just as you
                 would for a compiler.  So, your could do something like:
14:02     Griep| interpreter.References.Add(System.Reflection.Assembly.GetExecutingAssembly());
14:03     Griep| If myObject is an instance, you would likely need to set that
                 variable for the interpreter to be able to access it as such:
14:04     Griep|
14:04     Griep| interpreter.Declare(“myObject”, typeof(MyObject));
14:04     Griep| interpreter.SetValue(“myObject”, myObject);
14:05   Avictus| Thank you, that looks like exactly what I need.  I appreciate
                 the help.
14:06     Griep| You’re welcome!  Happy hunting.

1 thought on “Using boo as an embedded scripting application”

Comments are closed.