I am using IronRuby and trying to figure out how to use a block using the C # method.
This is the basic Ruby code I'm trying to emulate:
def BlockTest () result = yield("hello") puts result end BlockTest { |x| x + " world" }
My attempt to do the same with C # and IronRuby:
string scriptText = "csharp.BlockTest { |arg| arg + 'world'}\n"; ScriptEngine scriptEngine = Ruby.CreateEngine(); ScriptScope scriptScope = scriptEngine.CreateScope(); scriptScope.SetVariable("csharp", new BlockTestClass()); scriptEngine.Execute(scriptText, scriptScope);
BlockTestClass:
public class BlockTestClass { public void BlockTest(Func<string, string> block) { Console.WriteLine(block("hello ")); } }
When I run the C # code, I get an exception:
wrong number of arguments (0 to 1)
If I change the IronRuby script to the following, it will work.
string scriptText = "csharp.BlockTest lambda { |arg| arg + 'world'}\n";
But how do I get it to work with the original IronRuby script so that it becomes the equivalent of my original Ruby example?
string scriptText = "csharp.BlockTest { |arg| arg + 'world'}\n";
source share