[Ironruby-core] Getting started in VB?

Jimmy Schementi Jimmy.Schementi at microsoft.com
Thu Feb 26 00:54:26 EST 2009

A ScriptSource isn't compiled code yet, so you'll need to call ScriptSource.Execute() to actually run it. So, your example never ran the string in the "code" variable. Also, because your "action" variable contains code that hasn't been compiled, sending it to ObjectOperations.Call() will tell you "The method or operation is not implemented", because it can't do anything with non-compiled code. =) Lastly, in this scenario, there's no reason to use a string to grab the method, just to later call it with VB; you can do it all in VB. 

Here's a snippet of code which will grab a Ruby class, instantiate it, get a method on that class, and call it with arguments:

' Initialize the DLR
runtime = ScriptRuntime.CreateFromConfiguration()
Dim engine As ScriptEngine = runtime.GetEngine("Ruby")

' Execute a Ruby file

' Get a class and initialize it
Dim rubyClass As Object = runtime.Globals.GetVariable("MyController")
Dim rubyObject As Object = engine.Operations.CreateInstance(rubyClass)

' Get a method and call it
Dim rubyMethod As Object = engine.Operations.GetMember(rubyObject, "do_foo")
Dim params() As Integer = {1, 2}
Dim result As Object = engine.Operations.Call(rubyMethod, params)

' And lastly ...
Me.TextBox1.Text = result.ToString()

Hope that helps,

> -----Original Message-----
> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-
> bounces at rubyforge.org] On Behalf Of Mr X enterprises
> Sent: Wednesday, February 25, 2009 9:13 PM
> To: ironruby-core at rubyforge.org
> Subject: [Ironruby-core] Getting started in VB?
> I'd like to use IronRuby to make some applications extensible, but I'm
> having some problems getting started:
> Here's code I run when you click a button:
> runtime = ScriptRuntime.CreateFromConfiguration()
> runtime.ExecuteFile("MyController.rb")
> Dim engine As ScriptEngine = runtime.GetEngine("Ruby")
> Dim code As String
> code = String.Format("{0}.new.method :{1}", "MyController", "do_foo")
> Dim action As ScriptSource = engine.CreateScriptSourceFromString(code)
> Dim result As Object
> result = engine.Operations.Call(action, 1, 2)
> '^I get an exception saying: The method or operation is not
> implemented.
> Me.TextBox1.Text = result.ToString()
> Here is the ruby code:
> class MyController
>   def do_foo a, b
>     puts a, b
>   end
> end
> I've tried googleing the problem but I can't find any good tutorials.
> Any help?
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core

More information about the Ironruby-core mailing list