Skip to content

Instantly share code, notes, and snippets.

@matsadler
matsadler / ruby_preload
Last active October 3, 2018 08:33
Simple Ruby app preloader.
#!/usr/bin/env ruby
require "socket"
# This is a simple preloader for Ruby apps.
# Usage:
# Put this file somewhere in your PATH, named 'ruby_preloader' and make sure
# it's executable.
# Add whatever you need to load your app environment to a .preload.rb in the
# base directory of your project.
# Add #!/usr/bin/env ruby_preload as the first line of any script you want to
@matsadler
matsadler / url_fetcher.rb
Created March 10, 2015 10:13
Simple class to make http requests with multiple connections in separate threads
require "net/http"
# fetcher = FeedCrawler::URLFetcher.new("example.com")
# paths = ["/foo", "/bar", "/baz", "/qux"]
# results = fetcher.fetch(paths)
# results.each do |result|
# puts result
# end
#
class URLFetcher
@matsadler
matsadler / unix_socket_request.rb
Created July 1, 2014 10:53
Make a HTTP request to unicorn over a UNIX socket
require "socket"
# edit these
socket_path = "tmp/sockets/unicorn.sock"
host = "example.com" # HTTP 1.1 requires a Host header
path = "/"
# connect
sock = UNIXSocket.new(socket_path)
@matsadler
matsadler / kdtree.js
Last active December 20, 2015 06:39
Javascript k-d tree, a tree data structure for fast multidimensional nearest-neighbour lookups.
// dimensions should be the number of dimensions your points have
// points should be an array of arrays, the inner arrays representing points
// depth shouldn't be passed, it's used internally
// example:
// var tree = kdtree(2, [[1,2], [3,4], [5,6]]);
function kdtree(dimensions, points, depth) {
depth = depth || 0;
var axis = depth % dimensions,
node = {axis: axis};
@matsadler
matsadler / binary_search.rb
Last active December 18, 2015 11:19
Different binary search implementations in ruby with benchmarks.
# simple recursive algorithm
def binary_search_recursive(ary, value)
return nil if ary.empty?
pivot = ary.length / 2
pivot_value = ary[pivot]
if pivot_value < value
binary_search_recursive(ary[(pivot + 1)..-1], value)
elsif pivot_value == value
return pivot
else
@matsadler
matsadler / setenv.sh
Created May 22, 2013 09:47
Execute a command/script with environment variables read from a .env file. Use like `setenv mycommand arg1 arg2 etc` .env file should look like: VAR1=vlaue1 VAR2=value2
#!/usr/bin/env sh
set -e
dot_env=`cat .env`
env $dot_env $@
@matsadler
matsadler / ruby-2.0.0-in-detail.md
Last active December 14, 2015 16:29
Detailed rundown of many of the new features in Ruby 2.0.0.

Ruby 2.0.0 in detail

Keyword arguments

def wrap(string, before: "<", after: ">")
  "#{before}#{string}#{after}" # no need to retrieve options from a hash
end

# optional
@matsadler
matsadler / auto_pipeline.rb
Created November 28, 2012 09:32
Example of an auto-piplining HTTP client
require "net/http/persistent"
require "net/http/pipeline"
class DeferredProxy < BasicObject
def initialize(&generator)
@generator = generator
end
def method_missing(*args, &block)
@source ||= @generator.call
@matsadler
matsadler / sphinx_query_log.rb
Created November 9, 2012 12:51
simple ruby sphinx query log parser
require "time"
# Usage:
#
# SphinxQueryLog.each("path/to/query.log") do |entry|
# # do stuff with entry
# end
#
class SphinxQueryLog
@matsadler
matsadler / ruby_2_0_0_highlights.rb
Created November 2, 2012 14:13
A few examples of the ruby 2.0.0 preview 1 highlights given in http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/46348
# Ruby 2.0.0 preview 1 highlights
# A few examples of the highlights given in
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/46348
# Refinements
# ===========
# create a namespaced refinement
module NumberQuery