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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:15:32+00:00 2026-06-17T21:15:32+00:00

I am wondering how one would search through an array of hashes and return

  • 0

I am wondering how one would search through an array of hashes and return a value based on a search string. For example, @contacts contains the hash elements: :full_name, :city, and :email. The variable @contacts (I guess it would be an array) contains three entries (perhaps rows). Below is the code I have so far to conduct a search based on :city value. However it’s not working. Can anyone give me an idea what’s going on?

def search string
  @contacts.map {|hash| hash[:city] == string}
end
  • 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-17T21:15:33+00:00Added an answer on June 17, 2026 at 9:15 pm

    You should use select instead of map:

    def search string
      @contacts.select { |hash| hash[:city] == string }
    end
    

    In your code you tried to map (or transform) your array using a block, which yields boolean values. map takes a block and invokes the block for each element of self, constructing a new array containing elements returned by the block. As the result, you got an array of booleans.

    select works similar. It takes a block and iterates over the array as well, but instead of transforming the source array it returns an array containing elements for which the block returns true. So it’s a selection (or filtering) method.

    In order to understand the difference between these two methods it’s useful to see their example definitions:

    class Array
      def my_map
        [].tap do |result|
          self.each do |item|
            result << (yield item)
          end
        end
      end
    
      def my_select
        [].tap do |result|
          self.each do |item|
            result << item if yield item
          end
        end
      end
    end
    

    Example usage:

    irb(main):007:0> [1,2,3].my_map { |x| x + 1 }
    [2, 3, 4]
    irb(main):008:0> [1,2,3].my_select { |x| x % 2 == 1 }
    [1, 3]
    irb(main):009:0>
    
    • 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 iterators and was wondering how one would iterate through each
just wondering how it would be possible to recursively search through a website folder
I was wondering how one would go about developing a website from scratch with
In Business Intelligence Developer Studio, I'm wondering why one would want to create a
I'm working in the Symfony2 framework and wondering when would one use a Doctrine
I am wondering how one can search for all primitive float values that match
If one were to want to search OTHER websites for information, how would one
I am attempting to use a recursive function to search through a multidimensional array,
I'm wondering how one would go about redirecting the stdin of a script from
I am wondering how one gets output from SWI-Prolog when invoking it from the

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.