Forum: IronRuby Getting started in VB?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E87b7951bbf3a6c7ced3aa85b61bba20?d=identicon&s=25 Mr X Enterprises (x-enterprises)
on 2009-02-26 06:12
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?
E1deaac6561498a0e7472e6268ea9ccc?d=identicon&s=25 Jimmy Schementi (Guest)
on 2009-02-26 06:55
(Received via mailing list)
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
runtime.ExecuteFile("MyController.rb")

' 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,
~Jimmy
Cb51033949ffccd982ae32c9f890f25a?d=identicon&s=25 Tomas Matousek (Guest)
on 2009-02-26 07:44
(Received via mailing list)
This should work:

        Dim engine = IronRuby.Ruby.CreateEngine()
        engine.ExecuteFile("MyController.rb")
        Dim controllerClass =
engine.Runtime.Globals.GetVariable("MyController")
        Dim controller =
engine.Operations.CreateInstance(controllerClass)
        engine.Operations.InvokeMember(controller, "do_foo")

Tomas
E87b7951bbf3a6c7ced3aa85b61bba20?d=identicon&s=25 Mr X Enterprises (x-enterprises)
on 2009-02-26 08:09
Thank you guys so much! :D

Now I can continue on with this. :)

Thank you!
E87b7951bbf3a6c7ced3aa85b61bba20?d=identicon&s=25 Mr X Enterprises (x-enterprises)
on 2009-02-26 23:17
Sorry, I have another problem:

engine.Operations.CreateInstance(rubyClass)

It is saying that CreateInstance is not a member of
Microsoft.Scripting.Hosting.ObjectOperations.


There are Create() and CreateObjRef() functions.

Any help?

Thanks. :)
Cb51033949ffccd982ae32c9f890f25a?d=identicon&s=25 Tomas Matousek (Guest)
on 2009-02-27 00:39
(Received via mailing list)
Do you have the latest build? See
http://nightlybuilds.cloudapp.net/Project.aspx?project=dlr

Tomas
E87b7951bbf3a6c7ced3aa85b61bba20?d=identicon&s=25 Mr X Enterprises (x-enterprises)
on 2009-02-27 06:35
I got the newest build.
It said use Invoke() instead of Call(), so I used Invoke().


I get and exception for:
engine.Operations.Call(rubyMethod, params)

saying:
wrong number of arguments (1 for 2)


Thanks for all of your help. :)
E1deaac6561498a0e7472e6268ea9ccc?d=identicon&s=25 Jimmy Schementi (Guest)
on 2009-02-27 07:34
(Received via mailing list)
What does "params" contain? If I recall, the method you're calling
requires two parameters ...
________________________________________
From: ironruby-core-bounces@rubyforge.org
[ironruby-core-bounces@rubyforge.org] On Behalf Of Mr X Enterprises
[lists@ruby-forum.com]
Sent: Thursday, February 26, 2009 9:35 PM
To: ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] Getting started in VB?

I got the newest build.
It said use Invoke() instead of Call(), so I used Invoke().


I get and exception for:
engine.Operations.Call(rubyMethod, params)

saying:
wrong number of arguments (1 for 2)


Thanks for all of your help. :)
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core
E87b7951bbf3a6c7ced3aa85b61bba20?d=identicon&s=25 Mr X Enterprises (x-enterprises)
on 2009-02-27 07:55
Dim params() As Integer = {1, 2}
E87b7951bbf3a6c7ced3aa85b61bba20?d=identicon&s=25 Mr X Enterprises (x-enterprises)
on 2009-03-01 23:47
Nevermind, I figured it out. :)

I had to use parenthesesis in the def.

def do_foo(v1,v2)

Thanks for all of your help. :)
This topic is locked and can not be replied to.