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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:28:06+00:00 2026-06-07T02:28:06+00:00

class CartesianProduct include Enumerable # your code here end #Examples of use c =

  • 0
class CartesianProduct
include Enumerable
# your code here
end
#Examples of use
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
# [:a, 4]
# [:a, 5]
# [:b, 4]
# [:b, 5]
c = CartesianProduct.new([:a,:b], [])
c.each { |elt| puts elt.inspect }
# (nothing printed since Cartesian product
# of anything with an empty collection is empty)

I am new to ruby. And I understand how to define a instance method of Cartesian Product, but I have no clue to this. How should I construct the class object to fulfill the requirement.

  • 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-06-07T02:28:07+00:00Added an answer on June 7, 2026 at 2:28 am

    I wouldn’t use a class for that, but keeping the structure of the question, I’d write:

    class CartesianProduct
      include Enumerable
    
      def initialize(xs, ys)
        @xs = xs
        @ys = ys
      end
    
      def each
        return to_enum unless block_given?
        @xs.each do |x| 
          @ys.each { |y| yield [x, y] }
        end
      end
    end
    

    Instead, I’d simply write xs.product(ys) or build my own Array#lazy_product if laziness were important (see this ticket).

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

Sidebar

Related Questions

class A @@ololo = 1 end A::ololo A.new.ololo NoMethodError: undefined method `ololo' okey. I
Class 1 private void checkDuplicateCustomer(BulkCustomerVO bulkCustomerVO) { PagedDuplicateCustomerVO duplicateCustomerVO = new PagedDuplicateCustomerVO(); duplicateCustomerVO.setCustomer(bulkCustomerVO.getCustomerVO()); duplicateCustomerVO
class A attr_accessor :dab .... end Now I have an array of instances of
I saw this similar question here but can't figure out how to use Contains
class MyClass<T> where T: new() { public Test() { var t = new T();
class Person < ActiveRecord::Base validates :terms_of_service, :acceptance => true end In the above, what
class Size{ Size(){ } //public final Size SMALL = new Size();//infinite loop final Size
Class A { string name; IList<A> minorList = new List<A>(); } IList<A> majorList =
class ZiggyTest{ public static void main(String[] args){ RunnableThread rt = new RunnableThread(); Thread t1
class Object { }; Class Derived : public Object { }; boost::shared_ptr<Object> mObject(new Derived);

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.