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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:38:10+00:00 2026-05-21T06:38:10+00:00

How can I compare classes in Ruby or in other words how can I

  • 0

How can I compare classes in Ruby or in other words how can I translate this Java code into Ruby?

Class<?> clazz = ...;
if (clazz == String.class) {
  ...
} else if (clazz == Integer.class) {
  ...
}

To clarify: I do not want to compare object instances or check if an object is an instance of a class.

EDIT: I do not want to compare object instances or check if an object is an instance of a class, i.e. is_a? and kind_of? don’t help me.

  • 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-21T06:38:10+00:00Added an answer on May 21, 2026 at 6:38 am

    The literal translation of your Java code would be something like

    klass = ...
    
    if klass == String
      ...
    elsif klass == Integer
      ...
    end
    

    Or, more idiomatically

    klass = ...
    
    case
    when klass == String
      ...
    when klass == Integer
      ...
    end
    

    Or maybe

    klass = ...
    
    DISPATCH_TABLE = {
      String => -> { ... },
      Integer => -> { ... }
    }
    
    DISPATCH_TABLE[klass].()
    

    However, Ruby is an object-oriented language, and in an object-oriented language this simply doesn’t make sense. You would instead just write

    class String
      def do_something
        ...
      end
    end
    
    class Integer
      def do_something
        ...
      end
    end
    

    and the language will perform the dispatching for you. This is called polymorphism and is supported by pretty much every object-oriented language and many non-object-oriented ones as well.

    This particular transformation is one of the fundamental Refactorings described in Martin Fowler’s book (p. 255), it is called the Replace Conditional With Polymorphism Refactoring.

    The biggest problem with providing a sensible solution to your problem is that you don’t tell us what the problem is. You only tell us what the solution is. Or, more precisely, you tell us what you think the solution is in Java, and you somehow expect that the solution in Ruby would be exactly 100% identical, even though the languages couldn’t be more different.

    To provide a good solution, we need to know the problem first. In other words: the most relevant parts of your question are the …s

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

Sidebar

Related Questions

How can i compare 2 classes? The following if statement never passes although class
Below is selected code from one of 5 classes for this assignment. Each class
In Java, nested classes can be either static or not. If they are static
I wrote two matrix classes in Java just to compare the performance of their
is there a way l can compare two columns | fields in django like
I would like to know if there is anyway I can compare two columns
I have an IEnumerable, listOfOnes, and an IEnumerable, listOfTwos. Assuming that I can compare
Can I compare a commit to a branch and see if there is a
How can I compare strings in a case insensitive way in Python? I would
How can i compare two rows of datatable in QTP

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.