Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 646513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:37:12+00:00 2026-05-13T21:37:12+00:00

I’m using IronRuby and trying to come up with a seamless to pass a

  • 0

I’m using IronRuby and trying to come up with a seamless to pass a block to C#. This question is a follow on from How to use an IronRuby block in C# which indicates blocks cannot be passed between IronRuby and C#.

My subsequent question is as to whether there’s a way to acheive the same goal using a Ruby wrapper method to make the block into a lambda or Proc object and pass it to C#?

Some code that’s along the lines of what I’d like to be able to do is below. The code doesn’t work but hopefully the intent is clear enough.

string scriptText =
    @"def BlockTest
     result = csharp.BlockTest somehow-pass-the-block ("hello")
     puts result
    end
    BlockTest { |arg| arg + 'world'}";

Console.WriteLine("Compiling IronRuby script.");
ScriptEngine scriptEngine = Ruby.CreateEngine();
ScriptScope scriptScope = scriptEngine.CreateScope();
scriptScope.SetVariable("csharp", new BlockTestClass());
scriptEngine.Execute(scriptText, scriptScope);

The C# BlockTest class is:

public class BlockTestClass
{
    public void BlockTest(Func<string, string> block)
    {
        Console.WriteLine(block("hello "));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:37:13+00:00Added an answer on May 13, 2026 at 9:37 pm

    A block is a mysterious thing that doesn’t really exist in the CLR. Ruby does however have a proc which is analogous to CLR delegates.

    For example, this should work fine:

    C#:

    public class SomeClass {
        public static void Run(Func<int, int> func) { ... }
    }
    

    Ruby:

    proc = lambda { |i| i + 10 }
    SomeClass.Run(proc)
    

    There are several ways to create new procs (many people prefer Proc.new, but it doesn’t quite work the same way as a C# function so I prefer lambda), but the key point is this:

    lambda (and Proc.new) is a function – you pass the function a block, and it generates a new Proc object which wraps the block.

    There is also a way of implicitly converting blocks to procs when they are passed to a function, using &.

    Ruby:

    def each
      yield 1
    end
    

    is the same as

    def each(&block)
      block.call 1
    end
    

    The & takes the block which has been passed to the method (which would have otherwise been implicitly called by yield) and converts it into a Proc. Once it is a proc, you can pass it around, stick it in arrays, or pass it down to C#.

    So, to call your C# method, you have two options:

    1. Create a wrapper method which takes the block, and uses & to convert it to a proc. You can then call the underlying C# method with this proc

    2. Use lambda or Proc.new in ruby to create a proc directly, and pass it to the C# method.

    Examples:

    C# Code (common):

    public static class MyClass
    {
        public static void Print(Func<int, int> converter)
        {
            Console.WriteLine("1: {0}", converter(1));
        }
    }
    

    Ruby Code 1 (wrapper):

    class MyClass
      class << self # this is only needed to alias class (static) methods, not for instance methods
        alias_method :original_print, :print
      end
    
      def self.print(&block)
        original_print block
      end
    end
    
    MyClass.print {|i| i * 10}
    

    Ruby Code 2 (direct):

    MyClass.print lambda{|i| i * 10}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.