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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:33:16+00:00 2026-05-23T02:33:16+00:00

How do I create a method like: def process_by_type *things things.each {|thing| case thing.type

  • 0

How do I create a method like:

def process_by_type *things

  things.each {|thing|
    case thing.type
      when String

      when Array

      when Hash

      end

    end
  }
end

I know I can use kind_of?(Array), etc. But I think this would be cleaner, and I cant’ find a method for it on the Class:Object documentation page.

  • 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-23T02:33:18+00:00Added an answer on May 23, 2026 at 2:33 am

    Using the form of case statement like what you’re doing:

    case obj
      when expr1
        # do something
      when expr2
        # do something else
    end
    

    is equivalent to performing a bunch of if expr === obj (triple-equals comparison). When expr is a class type, then the === comparison returns true if obj is a type of expr or a subclasses of expr.

    So, the following should do what you expect:

    def process_by_type *things
    
      things.each {|thing|
        case thing
          when String
            puts "#{thing} is a string"
          when Array
            puts "#{thing} is an array"
          when Hash
            puts "#{thing} is a hash"
          else
            puts "#{thing} is something else"
        end
      }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to create a method like so: def email_is_junk(email_address) end Where it returns
I'd like to create a generic method for converting any System.Enum derived type to
I would like to create a String.replaceAll() method in JavaScript and think using a
My create user method in the users_controller.rb looks like: def process_login is_login_valid(params[:user][:user_name], params[:user][:password]) if
I have a somewhat special use case, where I'd like to create a method
Is it possible to create a method like BitConverter.GetBytes() that accepts as input also
I would like to create a method in a base generic class to return
I would like to create a method that takes an event as an argument
I'd like to use this method to create user-friendly URL. Because my site is
Is it possible to create a generic method with a signature like public static

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.