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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:19:14+00:00 2026-06-16T23:19:14+00:00

Trying to get the values out of a collection that could be either an

  • 0

Trying to get the values out of a collection that could be either an Array or a Hash, but switching based on type seems awkward:

def values_from_collection(array_or_hash)
  case array_or_hash
  when array_or_hash.is_a? Array
      array_or_hash
  when array_or_hash.is_a? Hash
      array_or_hash.values
  end
end 

Is seems like there should be a single interface/method that both classes support, but nothing obvious stands out in Enumerable. Is there a standard way to accomplish this?

  • 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-16T23:19:15+00:00Added an answer on June 16, 2026 at 11:19 pm

    Well, it doesn’t seem so to me. Hash and Array are very different data structures. Why do you think they should support this?

    Anyway, you could, for example, monkey-patch Array class to add values method

    class Array
      def values
        self
      end
    end
    

    And then your method is greatly simplified:

    def values_from_collection(array_or_hash)
      array_or_hash.values
    end 
    

    But this is worse than branching, in my opinion.

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

Sidebar

Related Questions

I have a json file that I'm trying to get values out of. One
Trying to get a value out of 2d array inside of a hash and
I'm trying to get the values from a pointer to a float array, but
I am trying to sort array/lists/whatever of data based upon the unicode string values
I'm trying to have a (hash-based) Multimap with a (hash-based) Multiset of values for
I'm trying to create a collection of values that each correspond to an action.
Im trying to work out how to get the value from one setting in
I'm trying to figure out how to get the value from a method in
I'm trying to figure out how to get the changed value to submit to
I'm trying to get values from nsdata class and doesn't work. here is my

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.