Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
technicalpickles / gist:2026264
Created March 13, 2012 02:44
Fun with memory & profiling
require 'thread'
class Worker
def initialize(count = 1)
@queue, @closing, @threads, @mutex = Queue.new, false, [], Mutex.new
add_worker(count)
end
def add_worker(count = 1)
@mutex.synchronize do
@dchelimsky
dchelimsky / output.txt
Created March 16, 2012 15:28
RSpec is not the reason your rails test suite is slow
[david: compare]$ # at this point we have a stock rails app with no minitest tests and one pending rspec example
[david: compare]$
[david: compare]$ time rake test
Run options:
# Running tests:
Finished tests in 0.030419s, 0.0000 tests/s, 0.0000 assertions/s.
def lazy
yield
rescue NameError
Module.new
end
begin
raise "foo"
rescue lazy { Foo }, lazy { Exception }
end
@erikh
erikh / hack.sh
Created March 31, 2012 07:02 — forked from DAddYE/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@pmarreck
pmarreck / is_json.rb
Created May 23, 2012 14:57
Apparently this is an actual JSON validator, written in late-model Regexp, using the Ruby interpreter.
# encoding: utf-8
module Constants
JSON_VALIDATOR_RE = /(
# define subtypes and build up the json syntax, BNF-grammar-style
# The {0} is a hack to simply define them as named groups here but not match on them yet
# I added some atomic grouping to prevent catastrophic backtracking on invalid inputs
(?<number> -?(?=[1-9]|0(?!\d))\d+(\.\d+)?([eE][+-]?\d+)?){0}
(?<boolean> true | false | null ){0}
(?<string> " (?>[^"\\\\]* | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* " ){0}
@cflipse
cflipse / Post.rb
Last active October 1, 2019 15:56
External Validations using only ActiveModel
require 'active_model'
# Most of this is the basic boilerplate described in the docs for active_model/errors; ie, the bare minimum
# a class must have to use AM::Errors
class Post
extend ActiveModel::Naming
attr_reader :errors
attr_accessor :title, :author, :publication_date

This is a distillation of a technique we use internally at Basho to reduce the pain of cloning some of our larger repos. It relies a bit on shell scripts and environment variables, but it can be run more directly, as I show below.

  1. Create a bare git repository in some directory, e.g. /var/lib/gitcache.

    git init --bare /var/lib/gitcache
  2. Add all repositories you want to cache as remotes to the git repository, e.g.

@mislav
mislav / easy_way.rb
Last active May 20, 2020 13:48
RESOLVE SHORT URLS before storing. Short URLs are for microblogging; you should never actually keep them around.
require 'net/http'
# WARNING do not use this; it works but is very limited
def resolve url
res = Net::HTTP.get_response URI(url)
if res.code == '301' then res['location']
else url.to_s
end
end
@mislav
mislav / geoip_service.rb
Created September 3, 2012 11:48
Simple GeoIP service class using freegeoip.net and Faraday
require 'faraday_middleware'
require 'hashie/mash'
# Public: GeoIP service using freegeoip.net
#
# See https://github.com/fiorix/freegeoip#readme
#
# Examples
#
# res = GeoipService.new.call '173.194.64.19'