Skip to content

Instantly share code, notes, and snippets.

module Kernel
def system(*args)
rd, wr = IO.pipe
# Create a new subprocess that will just exec the requested program.
pid = fork do
# The sub-process closes its copy of the reading end of the pipe
# because it only needs to write.
rd.close
@jstorimer
jstorimer / port_scanner.rb
Created August 30, 2012 03:40
Simple, parallel port scanner in Ruby built with connect_nonblock and IO.select.
require 'socket'
# Set up the parameters.
PORT_RANGE = 1..512
HOST = 'archive.org'
TIME_TO_WAIT = 5 # seconds
# Create a socket for each port and initiate the nonblocking
# connect.
sockets = PORT_RANGE.map do |port|
Rehearsal -------------------------------------------------
string interp 0.200000 0.000000 0.200000 ( 0.202780)
array join 0.520000 0.020000 0.540000 ( 0.540294)
---------------------------------------- total: 0.740000sec
user system total real
string interp 0.210000 0.000000 0.210000 ( 0.216187)
array join 0.520000 0.000000 0.520000 ( 0.518884)
Jesse Storimer is the author of <a href="http://workingwithunixprocesses.com">Working With Unix Processes</a> and currently works at <a href="http://shopify.com">Shopify</a>. You can find him online as @jstorimer on Twitter/Github.</p>

RocketSocket

This library was created out of my frustrations working with the Ruby's built-in Socket classes when writing Working With Sockets. It provides two functions: 1) A better way of building sockets with the common options that are more configurable/portable, and 2) A built-in way of framing messages in an safe and efficient manner. Nothing fancy.

RocketSocket::Build

socket = RocketSocket::Build.host('0.0.0.0').port(8080).ssl.read_timeout(60).write_timeout(10).listen(512).tcp
socket.write_timeout #=> 10
require 'socket'
# Ruby 1.8
Process.getrlimit(Process::RLIMIT_CORE)
Process.getrlimit(Process::RLIMIT_NOFILE)
# Ruby 1.9
Process.getrlimit(:CORE)
Process.getrlimit(:NOFILE)
require 'active_support/core_ext/module/delegation'
Person = Struct.new(:name, :email)
class Designer
attr_reader :person
delegate :name, :email, :to => :person
def initialize(person)
@person = person
require 'socket'
require 'kgio'
socket = Kgio::TCPServer.new '127.0.0.1', 4481
concurrency = 3
child_pids = []
sig_queue = []
concurrency.times do
# Put filenames on the pipe to have them loaded
# $ echo test/unit/foo_test.rb > zippy.pipe
require 'benchmark'
#
# load rails and the gems
result = Benchmark.realtime { require './config/application' }
puts "Loading gems took #{result}s"
# open pipe
require 'benchmark'
# load Rails and gems
puts Benchmark.realtime { require './config/application' }
# open fifo
pipe = open 'zippy.pipe', 'r+'
loop do
filename = pipe.gets.strip