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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I want use html5's new tag to play a wav file (currently only supported
In order to apply a triggered animation to all ToolTip s in my app,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I've got a string that has curly quotes in it. I'd like to replace

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.