Skip to content

Instantly share code, notes, and snippets.

View return_object_bench.rb
class Add
attr_reader :result, :is_negative
def initialize(a, b)
@result = a + b
@is_negative = @result < 0
def add(a,b)

Ten seconds to ponder if a thread is worth it


A userstyle that makes you wait ten seconds before entering a Hacker News thread. I use stylus to manage mine.

.subtext {
  display: inline-block;
 background: linear-gradient(to left, transparent 50%, #f60 50%) right;
View bling.js
/* bling.js */
window.$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn);
NodeList.prototype.__proto__ = Array.prototype;
View enumerable_example.rb
class Coffee
attr_accessor :name
attr_accessor :strength
def initialize(name, strength)
@name = name
@strength = strength
def <=>(other_coffee)
sstephenson / Simple
Created Apr 11, 2013
Simple file/stream encryption using OpenSSL
View Simple

Simple file/stream encryption using OpenSSL

Create and store a 512-byte random encryption key named secret:

$ mkkey secret

Encrypt the contents of file with the secret key and write it to file.enc:

$ encrypt secret < file > file.enc
madrobby / gist:4161897
Created Nov 28, 2012
Retina screen media query
View gist:4161897
@media (min--moz-device-pixel-ratio: 1.5),
(-o-min-device-pixel-ratio: 3/2),
(-webkit-min-device-pixel-ratio: 1.5),
(min-device-pixel-ratio: 1.5),
(min-resolution: 144dpi),
(min-resolution: 1.5dppx) {
/* Retina rules! */
jeremy / gist:4035286
Created Nov 7, 2012
View gist:4035286

We've seen lots of Ruby feature requests about converting an Enumerable to a Hash: to_h, map_hash, map_to, each_with_hash, etc. Let's look at one common, simple case of this.

Building a key/value mapping from a collection of keys is awkward. We commonly see Hash[* { |element| [element, calculate(element)] }] or collection.each_with_object({}) { |element, hash| hash[element] = calculate(element) }. Both are verbose. They require boilerplate code that's not relevant to the programmer's intent: to associate an enumerable of keys with calculated values.

Ruby has the idea of an association already: a key and value paired together. It's used by Array#assoc to look up a value from a list of pairs and by Hash#assoc to return a key/value pair. Building up a mapping of key/value pairs is associating keys with values.

So! Consider Enumerable#associate which builds a mapping by associating keys with values:

# Associate filenames with URLs. Before:
View clean_unicode.rb
# encoding: binary
# Removes any bytes from a string that are not valid UTF-8
class Cleaner
attr_reader :bytes, :buffer, :outstanding
def self.clean(str)
new.tap { |c| c << str }.to_s
rmurphey / gist:3086328
Created Jul 10, 2012
What's wrong with Netmag's "Optimize your JavaScript" post
View gist:3086328

What's wrong with Netmag's "Optimize your JavaScript" post

Update: The original post on Netmag has been updated since this was written.

I tweeted earlier that this should be retracted. Generally, these performance-related articles are essentially little more than linkbait -- there are perhaps an infinite number of things you should do to improve a page's performance before worrying about the purported perf hit of multiplication vs. division -- but this post went further than most in this genre: it offered patently inaccurate and misleading advice.

Here are a few examples, assembled by some people who actually know what they're talking about (largely Rick Waldron and Ben Alman, with some help from myself and several others from the place that shall be unnamed).

Things that are just plain wrong

jlong / uri.js
Created Apr 20, 2012
URI Parsing with Javascript
View uri.js
var parser = document.createElement('a');
parser.href = "";
parser.protocol; // => "http:"
parser.hostname; // => ""
parser.port; // => "3000"
parser.pathname; // => "/pathname/"; // => "?search=test"
parser.hash; // => "#hash"; // => ""