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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:38:32+00:00 2026-05-25T17:38:32+00:00

I’m new to ruby, and I’ve been googling for hours, but I can’t figure

  • 0

I’m new to ruby, and I’ve been googling for hours, but I can’t figure this one out. It looks like it should be really easy so I’m getting pretty frustrated.

I’m working in ruby and need to compare 2 arrays of symbols for a true or false return.

array1 = [:a, :c]
array2 = [:a, :b, :c]

The comparison I need to do, is to see if array2 includes all the elements of array1.
In this case array2 includes array1, but array1 does not include array2.

I tried:

array2.to_s.include?(array1.to_s) 

Which only returns true if they are in the same order because it needs to convert to a string for comparison. So as is it returns false (not what I’m looking for), but if array2 = [:a, :c, :b] it would be true. Is there a more appropriate way of making this comparison?

  • 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-25T17:38:32+00:00Added an answer on May 25, 2026 at 5:38 pm

    venj’s answer is fine for small arrays but might not perform well (this is debatable) for large arrays. Since you’re basically doing a set operation (“is Set(array1) a subset of Set(array2)?”), it makes sense to see how Ruby’s own Set library does this. Set has a subset? method, and taking a peek at its source we see that it’s short and sweet:

    def subset?(set)
      set.is_a?(Set) or raise ArgumentError, "value must be a set"
      return false if set.size < size
      all? { |o| set.include?(o) }
    end
    

    We could just instantiate two Set objects from the arrays and call it a day, but it’s just as easy to distill it into a oneliner that operates directly on them:

    array1 = [:a, :c]
    array2 = [:a, :b, :c]
    
    array1.length < array2.length && array1.all? {|el| array2.include? el }
    # => true
    
    array1 << :z
    array1.length < array2.length && array1.all? {|el| array2.include? el }
    # => false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.