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 6808655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:58:58+00:00 2026-05-26T19:58:58+00:00

I would like to write a code in Ruby that behaves like this C#

  • 0

I would like to write a code in Ruby that behaves like this C# code.

It receives a candidate topology set and a world set, and tests if the candidate topology is a topology in respect to the world.

In C# using LINQ features it looks like this:

public static bool IsTopology<T>(IEnumerable<IEnumerable<T>> candidate, IEnumerable<T> world)
{
    IEqualityComparer<IEnumerable<T>> setComparer =
        new SetComparer<T>();

    if (!candidate.Contains(Enumerable.Empty<T>(), setComparer) ||
        !candidate.Contains(world, setComparer))
    {
        return false;
    }

    var pairs =
        from x in candidate
        from y in candidate
        select new {x,y};

    return pairs.All(pair => candidate.Contains(pair.x.Union(pair.y), setComparer) &&
        candidate.Contains(pair.x.Intersect(pair.y), setComparer));
}

public class SetComparer<T> : IEqualityComparer<IEnumerable<T>>        
{
    public bool Equals (IEnumerable<T> x, IEnumerable<T> y)
    {
        return new HashSet<T>(x).SetEquals(y);
    }

    public int GetHashCode (IEnumerable<T> obj)
    {
        return 0;
    }
}

The features I am looking for are the following:

  • An ability to plug an equality comparer to methods

  • An ability to use nested maps (and anonymous types)

  • An ability to compare arrays as sets (not very important, in C# it lacks a bit…)

I believe that ruby has the features and am very interested to see how the equivalent code would look like.

  • 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-26T19:58:59+00:00Added an answer on May 26, 2026 at 7:58 pm

    I translated your code to ruby (and rewritten it a bit):

      # candidate - Enumerable of Enumerable; world - Enumerable; &block - comparer of two sets.
      def topology? candidate, world, &block
        require 'set'
        # you can pass block to this method or if no block passed it will use set comparison
        comparer = block || lambda { |ary1,ary2| ary1.to_set.eql?(ary2.to_set) }
        # create lambda-function to find a specified set in candidate (to reuse code)
        candidate_include = lambda { |to_find| candidate.find {|item| comparer.(item, to_find) } }
    
        return false if( !candidate_include.( []) || !candidate_include.( world) )
    
        pairs = candidate.to_a.repeated_permutation(2)
    
        pairs.all? do |x,y| x_set = x.to_set; y_set = y.to_set
            candidate_include.(x_set & y_set) && # or x_set.intersection y_set
            candidate_include.(x_set | y_set) # or x_set.union y_set
        end
      end
    

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would having a nice little feature that makes it quicker to write code like
I'd like to write a function that would have some optional code to be
I'm trying to write some LINQ To SQL code that would generate SQL like
I would like to write a ruby script that will save the Json object
I have a quad core machine and would like to write some code to
I would like to write some Objective-C code for distribution. What is the best
Can .NET (managed code) read and write to CouchDB? I would like to build
I would like to write some data to a file in Ruby. What is
I would like to write a plug-in that will allow a custom written CRM
I have recently designed a web application that I would like to write in

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.