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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:40:34+00:00 2026-06-12T05:40:34+00:00

I have an array of arrays like so: [[1, dog], [2, cat], [2, bird],

  • 0

I have an array of arrays like so: [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]]. I want to check whether the larger array contains arrays of a given number without regard to the animal element of the array. I also do not want to loop through the array because this would become computationally heavy.

So something like @boolean = bigArray.include?([2, *]), except something that actually works…

  • 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-12T05:40:36+00:00Added an answer on June 12, 2026 at 5:40 am

    You have two possible solutions. Detect the element in the array or convert the array into a Hash

    1. Use Enumerable#detect, it will stop as soon as it finds a match and return it, nil otherwise.

    > a = [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]]
     => [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]] 
    > a.detect{ |(n, _)| n == 2 }
     => [2, "cat"]
    > a.detect{ |(n, _)| n == 10 }
     => nil 
    

    If you want to add this to the Array class like your example, and force it to return a boolean, do this:

    class Array
      def custom_include?(num)
        !!detect{ |(n, _)| num == n }
      end
    end
    

    Example:

    > a.custom_include?(2)
     => true 
    > a.custom_include?(10)
     => false
    

    2. If you don’t care about the colliding keys, you could convert the array into a Hash and see if the key exists.

    > a = [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]]
     => [[1, "dog"], [2, "cat"], [2, "bird"], [3, "monkey"]] 
    > Hash[a][2]
     => "bird"
    > Hash[a][10]
     => nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array that looks like: a = [cat, dog, cat, mouse,
i have two arrays like this first array Array ( [0228] => Array (
I have 3 arrays like this: 1st Array ( [0695] => Array ( [loan_number]
I have arrays like this. [11] => Array ( [transactioncurrencyid] => Array ( [!name]
i have an array $mainArray of arrays and i would like to remove /
Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
I have an array of array like below with all numeric values. I want
I have an array with multiple sub-arrays like this: Array ( [0] => Array
I have an array of arrays. The inner array looks like this. Array (
I have an array of arrays that I want to use KVC on (at

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.