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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:43:33+00:00 2026-06-04T15:43:33+00:00

I have in mind a design problem that does not look like complex, but

  • 0

I have in mind a design problem that does not look like complex, but I can’t find by myself a way to solve it. I would like to follow as much as possible the DRY principle.

I have got two functions. One is the basic version of the algorithm, the other one is an improvement with parallel programming. They are two, and my aim is to write only one of them with an additional parameter, e.g., “parallel”. Here the pseudo codes.

fun1 ()
    loop for par1 times
        do_work()

fun2 ()
    loop for par1 times
        run new thread
            do_work()

Currently I have coded

if parallel == 0
    fun1 ()
else
    fun2 ()

What I want is something like

fun ()
    loop for par1 times
        run new thread if parallel > 0 #Ruby's syntax-like
            do_work()

I was wondering if Ruby may help me solving this issue. I am a newbie of Ruby so I am not sure its functional programming could make the difference.

  • 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-04T15:43:35+00:00Added an answer on June 4, 2026 at 3:43 pm

    Just like this:

    def fun(parallel=false)
      if parallel
        # called as fun(true)
      else
        # called as fun
      end
    end
    

    parallel=false sets the default argument value if no argument is passed.

    Or you could use an options hash to pass the parameters more explicitly

    def fun(options={})
      parallel = option[:parallel] || false
      if parallel
        # called as fun(parallel: true)
      else
        # called as fun
      end
    end
    

    Another approach is to always run do_work() in a thread and to wait for the thread to finish depending on the parallel parameter.

    def fun(parallel=false)
      workers = []
      par1.times do
        worker = Thread.new { do_work() }
        worker.join if parallel
        workers << worker
      end
      workers.each(&:join)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Django, but the application that I have in mind might end
I'm new to c++ but have set my mind on a specific task that
I have a project in mind that will offer users their own domain, like
I'm building a database and have run into a problem that I can't seem
Here is what I have in mind: 1) Create a service that will run
This is what I have in mind: Can anyone point out some relevant resources?
I have a project in mind that will require the majority of queries to
I have a specific case in mind, but the question applies in general too.
I have a question about iPhone app design approach... I have in mind to
I have a dilemna that needs I've been thinking a while but still haven't

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.