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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:18:53+00:00 2026-05-30T15:18:53+00:00

I have a bunch of functions that take in either an array of objects,

  • 0

I have a bunch of functions that take in either an array of objects, or a single object designed to be treated as an array containing one object, and I am looking for a cleaner way to accomplish this. Basically, I want to know how I could make the unless part in the following function more concise:

def foo(bar_or_bars)
  unless bar_or_bars.is_a?(Array)
    bar_or_bars = [bar_or_bars]
  end
  bar_or_bars.each { |baz| ... }
end

Any help will be appreciated! Thanks.

  • 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-30T15:18:55+00:00Added an answer on May 30, 2026 at 3:18 pm

    First thing you could do is to write the unless logic in a single line:

    bars = bar_or_bars.is_a?(Array) ? bar_or_bars : [bar_or_bars]
    

    As you see, I give it a new name here, as it’s no longer a bar or bars, it’s now definitely a collection.

    The problem with this and your original approach is that although your function could work on any Enumerable, you will force your users to give you an argument of a specific type, which breaks duck typing.

    A neat trick to partially solve that issue is the following:

    def foo(bar_or_bars)
      bars = [*bar_or_bars]
      bars.each { |baz| ... }
    end
    

    I wouldn’t exactly call that readable, though. It actually smells a lot like bad API design. Probably you should better take multiple arguments like this:

    def foo(*bars)
      bars.each { |baz| ... }
    end
    

    And let the caller decide whether he wants to pass a single object or an array:

    foo("XYZ")
    ary = ["abc", "def"]
    foo(*ary)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of functions that I want to put in either a
I have a bunch of overloaded functions that operate on certain data types such
I am working on a project where I have a bunch of functions that
I have a bunch of Javascript functions that look like the following: function generateBusinessImage
Let's say I have a bunch of related functions that have no persistent state,
I have a bunch of functions that map to and from some codes defined
Say i have a bunch of functions that will be using int = price;
I have a bunch of user-defined functions that are frequently used in mathematica. I
In a multithreaded application. I have a bunch of function that loop through a
I have a function that I have used a bunch of times in various

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.