I'm using IronRuby and trying to work out how to use a block with a C# method.
This is the basic Ruby code I'm attempting to emulate:
def BlockTest ()
result = yield("hello")
puts result
end
BlockTest { |x| x + " world" }
My attempt to do the same thing with C# and IronRuby is:
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);
The BlockTestClass is:
public class BlockTestClass
{
public void BlockTest(Func<string, string> block)
{
Console.WriteLine(block("hello "));
}
}
When I run the C# code I get an exception of:
wrong number of arguments (0 for 1)
If I change the IronRuby script to the following it works.
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's the equivalent of my original Ruby example?
string scriptText = "csharp.BlockTest { |arg| arg + 'world'}\n";