Call IronRuby from C # with a delegate

Is it possible to call the IronRuby method from C # with a delegate as a parameter so that it yieldworks?

The following is the wrong number of arguments (1 for 0).

Action<string> action = Console.WriteLine;
var runtime = Ruby.CreateRuntime();
var engine = runtime.GetEngine("rb");
engine.Execute(@"
                 class YieldTest
                   def test
                     yield 'From IronRuby'
                   end
                 end
                ");
object test = engine.Runtime.Globals.GetVariable("YieldTest");
dynamic t = engine.Operations.CreateInstance(test);
t.test(action);
+5
source share
2 answers

I received a response from the main IronRuby team member Tomáš Matoušek on the IronRuby list that this is not possible. Nonetheless.

+1
source

I'm sure the Ruby block is not a C # delegate.
If you pass the delegate to Ruby, you must call it through the Invoke method for the delegate.
Here is a sample code:

var rt = Ruby.CreateRuntime();
var eng = rt.GetEngine("rb");
eng.Execute(@"
            class Blocktest
              def test(block)
                block.Invoke('HELLO From IronRuby')
              end
            end
            ");
dynamic ruby = eng.Runtime.Globals;
dynamic t = ruby.Blocktest.@new();
t.test(new Action<string>(Console.WriteLine));

# ruby ​​... .

+1

All Articles