Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

num_threads

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

num_threads

  • 0.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

num_threads

A simple short-hand for running a block of Ruby code in multiple concurrent threads.

array = Array.new(10)

10.threads do
  array.pop
  # interesting stuff in here
end

Huh?

Call the threads method on a Fixnum and pass it a block. It spawns that many threads, runs the block, then calls Thread#join on each thread to wait for it to finish.

How is this useful?

I was writing a lot of simple example code that spawned a few threads for Working With Ruby Threads. I got sick of writing this code:

threads = []

10.times do
  threads << Thread.new do
    # interesting stuff in here
  end  
end

threads.each(&:join)

The interesting stuff is hidden in too many layers of boilerplate. I wanted to write this:

10.threads do
  # interesting stuff in here
end

These two code examples are equivalent. That's the whole point of the gem. Now the interesting stuff is easier to see with less boilerplate surrounding it.

I use this for writing example code. I imagine could also be useful in writing one-off scripts.

Installation

Install with:

$ gem install num_threads

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

FAQs

Package last updated on 26 Feb 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc