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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:39:41+00:00 2026-06-01T13:39:41+00:00

Okay so this is a bit complex but I need to loop through this

  • 0

Okay so this is a bit complex but I need to loop through this hash to find if each element meets one of following conditions:

The Value is a String

or

The Value is a Hash, containing Strings, Not Hashes

or

The Value is a Hash, containing Strings and/or Hashes

or

The Value is an Array

UPDATE:

Something very strange… [EDIT: this is because #collect is returning the hash’s key-value pairs, which are arrays]

irb> my_hash['gd:name'].collect(&:class)
=> [Array, Array]

Looking at the hash below should the class of that inner element be a Hash, not an Array?

Inspected it looks like:

irb> my_hash['gd:name'].collect(&:inspect)
=> ["[\"gd:givenName\", {\"$t\"=>\"Thomas\"}]", "[\"gd:familyName\", {\"$t\"=>\"Chapin\"}]"]

BUT IT BEHAVES LIKE A HASH:

irb> my_hash['gd:name']['gd:givenName']
=> {"$t"=>"Thomas"}
irb> my_hash['gd:name']['gd:givenName']['$t']
=> "Thomas"

{
  "gd:etag"=>"\"Rnk7fjVSLyt7I2A9WhVQEU4KRQI.\"",
  "id"=>{
    "$t"=>"da513d38e88d949"
   },
  "gd:name"=>{
    "gd:givenName"=>{"$t"=>"Thomas"}, "gd:familyName"=>{"$t"=>"Chapin"}
  },
  "gd:phoneNumber"=>[
    {
      "rel"=>"mobile",
       "$t"=>"(480) 703-4887"
    }
  ], 
  "gd:email"=>[
    {
      "rel"=>"home",
      "primary"=>"true",
       "address"=>"tchapin@gmail.com"
    }, 
    {
      "rel"=>"work",
      "address"=>"tom@tornadodesign.com"}
  ]
}
  • 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-01T13:39:43+00:00Added an answer on June 1, 2026 at 1:39 pm

    Examine Hash values.

    Return true iff each:

    • Value is a string or an array   
    • Value is a hash that is non-empty and contains only strings and/or hashes

    A solution with some printing to show what’s happening:

    puts hash.each_pair.all?{|k,v|
      puts "key #{k} = value #{v} class #{v.class}"
      case v
      when String, Array
        true
      when Hash
        return false if v.empty? 
        v.each_pair.all?{|k2,v2|
          puts "inner key #{k2} = value #{v2} class #{v2.class}"
          case v2
          when String, Hash
            true
          else
            false
          end
        }
      else
        false
      end
    }
    

    If you don’t care to keep track of the keys, you can change #each_pair to #values

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

Sidebar

Related Questions

Okay this one may be a bit out from left field, but I'm going
Okay, I have done a bit of searching online and found this thread, but
Okay so this query should be easy but I'm having a bit of difficult.
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay this is a bit of a noobish question, but I ran across this
Okay, this is a bit abstract, but here goes: I'm creating a website and
Okay, this will be a bit hard to explain, but I'll try my best.
Okay, this is starting to drive me a little bit nuts. I've tried several
okay, i'm a bit befuddled with this error. What i'm trying to do here
Okay this may be a simple question but I have yet to come with

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.