[Ironruby-core] Accessing IronRuby method from C#

Robert Brotherus robert.brotherus at napa.fi
Thu Jan 24 03:47:14 EST 2008


Thanks John for your reply!
 
Still not getting my method calls to work from C# with lambda either:
 
[TestMethod]
public void AccessMethod3()
{
    ExecuteString(@"
        $m = lambda do |par|
            puts 'Hello from lambda: ' + par
        end
    ");
    Proc m = (Proc) GetGlobalVar("m"); // OK
    Operations.Call(m, "abc"); // InvalidCast: Cannot cast Proc to
BlockParam
    Delegate1 d = Operations.ConvertTo<Delegate1>(m); // OK
    d("abc"); // InvalidCast: Cannot cast Proc to BlockParam
}
 
Robert Brotherus
Software architect
Napa Ltd
Tammasaarenkatu 3, Helsinki FI-00180
P.O.Box 470, Helsinki FI-00181

Tel.          +358 9 22 813 1
Direct.      +358 9 22 813 611
GSM         +358 45 11 456 02 
Fax.         +358 9 22 813 800

Email: Robert.Brotherus at napa.fi <mailto:Robert.Brotherus at napa.fi>  
www.napa.fi <http://www.napa.fi/> 

 

  _____  

From: ironruby-core-bounces at rubyforge.org
[mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of John Lam (DLR)
Sent: 23. tammikuuta 2008 16:59
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Accessing IronRuby method from C#



Today we don't support unbound methods yet (what you are trying to do
via the call to the method function). If you want first class functions,
Proc.new and lambda work just fine today.

 

I'll let Tomas chime in on AccessMethod2().

 

Thanks,

-John

 

 

 

 

From: ironruby-core-bounces at rubyforge.org
[mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Robert
Brotherus
Sent: Tuesday, January 22, 2008 8:20 AM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] Accessing IronRuby method from C#

 

Greetings to the mighty IronRuby experts from a humble developer! :-) 

I am testing the latest CVS snapshot (v. 73, downloaded an hour ago) of
IronRuby. 

We are evaluating IronRuby for possible usage in WPF project. 

First, I want to test a simple case of defining a method in Ruby and
calling the method later from C#. 

Below I copy a test program that tries this in two ways (methods
AccessMethod1 and AccessMethod2). 
Both attempts fail with exceptions that are written as comments in the
code. 

I understand that the DLR and IronRuby are in a flux at the moment but I
still suppose there must be some working way to perform such trivial
interop task...?

Thanks in advance! 

Robert Brotherus
Software architect
Napa Ltd 

--------------------- 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Scripting; 
using Microsoft.Scripting.Hosting; 
using Ruby.Runtime; 
using Ruby.Builtins; 

namespace IronRubyTests 
{ 
    class Program 
    { 
        private IScriptEnvironment _scriptRuntime; 
        public IScriptEnvironment ScriptRuntime         { get { return
this._scriptRuntime; } } 
        public IScriptEngine RubyEngine                 { get { return
ScriptRuntime.GetEngine(typeof(RubyContext)); } }

        public ObjectOperations Operations              { get { return
RubyEngine.Operations; } } 
        public RubyExecutionContext ExecutionContext    { get { return
Ruby.IronRuby.GetExecutionContext(RubyEngine); } }

        public IAttributesCollection GlobalItems        { get { return
ExecutionContext.GlobalScope.Dict; } } 

        delegate void TestDelegate(); 

        static void Main(string[] args) 
        { 
            new Program().RunAllTests(); 
        } 

        public Program() 
        { 
            this._scriptRuntime = Ruby.IronRuby.CreateRuntime(); 
        } 

        public void RunAllTests() 
        { 
            List<TestDelegate> tests = new List<TestDelegate> { 
                new TestDelegate(SimpleTests), new
TestDelegate(AccessMethod1), new TestDelegate(AccessMethod2) 
            }; 
            foreach (TestDelegate test in tests) 
            { 
                Console.WriteLine("================== EXECUTING: {0}
====================", test.Method.Name); 
                try { test(); } 
                catch (Exception ex) { Console.WriteLine(ex); } 
            } 
        } 

        public IScriptScope ExecuteString(string code) 
        { 
            return ScriptRuntime.ExecuteSourceUnit( 
                RubyEngine.CreateScriptSourceFromString(code)); 
        } 

        public void SetGlobalVar(string varName, object value) 
        { 
 
ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)] =
value; 
        } 

        public object GetGlobalVar(string varName) 
        { 
            return
ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)]; 
        } 

        public object GetGlobalConst(string name) 
        { 
            return
GlobalItems.SymbolAttributes[SymbolTable.StringToId(name)]; 
        } 

        public void SimpleTests() 
        { 
            ExecuteString("puts 'RUBY SAYS HELLO'"); // OK 
            SetGlobalVar("x", 666); // OK 
            ExecuteString("$y=555"); // OK 
            ExecuteString("puts \"ruby: $x = #{$x}\""); // OK 
        } 

        public void AccessMethod1() 
        { 
            // Following fails with System.MissingMethodException:
undefined local variable or method 'method' for main:Object

            ExecuteString(@" 
                def sayHello 
                   puts 'Hello from method!' 
                end 

                $m = method(:sayHello) 
            "); 

            // Get the method object: 
            object m = GetGlobalVar("m"); 

            // Here call the method, etc. 
        } 

        public void AccessMethod2() 
        { 
            ExecuteString(@" 
              module MyMethods 
                def sayHello 
                   puts 'Hello from method!' 
                end 
              end 
            "); 

            RubyModule myModule = (RubyModule)
GetGlobalConst("MyMethods"); // OK 
            object sayHello = Operations.GetMember(myModule,
"sayHello"); // Fails at Assertion at
ActionBinber.UpdateSiteAndExecute()

            // Here call the method, etc. 
        } 

    } // class 

} // namespace 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/ironruby-core/attachments/20080124/da9f1249/attachment-0001.html 


More information about the Ironruby-core mailing list