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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:49:36+00:00 2026-05-29T09:49:36+00:00

Is there a good library (preferably gem) for doing class checking of an object?

  • 0

Is there a good library (preferably gem) for doing class checking of an object? The difficult part is that I not only want to check the type of a simple object but want to go inside an array or a hash, if any, and check the classes of its components. For example, if I have an object:

object = [
  "some string",
  4732841,
  [
    "another string",
    {:some_symbol => [1, 2, 3]}
  ],
]

I want to be able to check with various levels of detail, and if there is class mismatch, then I want it to return the position in some reasonable way. I don’t yet have a clear idea of how the error (class mismatch) format should be, but something like this:

object.class_check(Array) # => nil (`nil` will mean the class matches)
object.class_check([String, Fixnum, Array]) # => nil
object.class_check([String, Integer, Array]) # => nil
object.class_check([String, String, Array]) # => [1] (This indicates the position of class mismatch)
object.class_check([String, Fixnum, [Symbol, Hash]) # => [2,0] (meaning type mismatch at object[2][0])

If there is no such library, can someone (show me the direction in which I should) implement this? Probably, I should use kind_of? and recursive definition.

  • 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-29T09:49:38+00:00Added an answer on May 29, 2026 at 9:49 am

    Here is something you can start with

    class Object
      def class_check(tclass)
        return self.kind_of? tclass unless tclass.kind_of? Array
        return false unless self.kind_of? Array
        return false unless length == tclass.length
        zip(tclass).each { | a, b | return false unless a.class_check(b) }
        true
      end
    end
    

    It will return true if the classes match and false otherwise.

    Calculation of the indices is missing.

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

Sidebar

Related Questions

Is there a good library for converting SVG to HTML canvas that supports the
Is there a good library in c# that can check against a ftp and
Is there a good library for extracting text from a PDF? I'm willing to
Is there a good library (or wrapper to Java library) for graphs, and/or graph
Is there a good C# library for drawing/visualizing graphs? I'm talking about node-path visualization
Is there a well-known, portable, good library for C++ process management? I found a
Is there a good .NET file syncing library? It can be pretty basic and
is there a good and free PDF generator library for delphi ? I need
Following this question: Good crash reporting library in c# Is there any library like
I was wondering if there was a good way to monitor a document library

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.