Skip to content

Instantly share code, notes, and snippets.

# RVM support in Rails 3 app for multiple gemsets with Passenger
# config/setup_load_paths.rb
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
begin
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME']))
rvm_lib_path = File.join(rvm_path, 'lib')
$LOAD_PATH.unshift rvm_lib_path
require 'rvm'
RVM.use_from_path! File.dirname(File.dirname(__FILE__))
@jasonmorganson
jasonmorganson / Node.build-system
Created July 18, 2011 13:48
Node build "system" for Sublime Text 2
{
"cmd": ["killall node >> /dev/null; node ${file}"],
"selector": "source.javascript",
"path": "/usr/bin",
"shell": true
}
@punund
punund / getImageSize.coffee
Created November 11, 2012 19:57
Retrieves first chunk of data of remote image via HTTP GET to find out its size in pixels. PNG, JPEG
getImageSize = (image, cb) ->
fromHex = (hex) -> parseInt "0x#{hex}", 16
req = http.request image, (res) ->
res.setEncoding 'hex'
res.on 'error', (e) -> cb "getImageSize error (#{image}): " + e, null, image
res.on 'data', (chunk) ->
m = switch res.headers['content-type']
when 'image/jpeg'
chunk.match 'ffc0001108(....)(....)'
when 'image/png'
@CMCDragonkai
CMCDragonkai / ternary_comparison.hs
Last active October 31, 2015 00:33
Haskell: Ternary Comparison Operators (inspired by ternary choice operator http://zenzike.com/posts/2011-08-01-the-conditional-choice-operator)
{-
The comparison operators are non-associative and not composable.
Thus it is illegal to write: 1 < 2 < 3
The input types of < (Int) does not match the output type (Bool).
1 < 2 < 3 can however be written in math notation, because it denotes:
(1 < 2) && (2 < 3) or (1 < 2) ^ (2 < 3)
It is however possible to create a new ternary operator that supports such a
notation!
-}
@ricardobeat
ricardobeat / node-0.8.20-hangup.js
Created February 21, 2013 19:27
Silence socket hangup errors.
function hangups (req, res, next){
var reqd = domain.create()
reqd.add(req)
reqd.add(res)
reqd.on('error', function (error) {
if (error.code !== 'ECONNRESET') console.error(error, req.url)
reqd.dispose()
})
next()
}
var bases = require('bases');
var crypto = require('crypto');
// Returns a base-62 (alphanumeric only) string of the given length:
function randomStr(length) {
// We generate a random number in a space at least as big as 62^length,
// and if it's too big, we just retry. This is still statistically O(1)
// since repeated probabilities less than one converge to zero. Hat-tip to
// a Google interview for teaching me this technique! ;)
### define function variable before block to avoid code being appended to closing part of JSDoc comment ###
cube = ''
###*
* Funtion to calculate cube of input
* @param {number} Number to operate on
* @return {number} Cube of input
###
cube = (x) -> x*x*x
@aseemk
aseemk / randomStr.js
Created July 12, 2012 05:00
Random alphanumeric (base-62) strings in Node.js, cryptographically strong
var bases = require('bases');
var crypto = require('crypto');
// Returns a base-62 (alphanumeric only) string of the given length:
function randomStr(length) {
// We generate a random number in a space at least as big as 62^length,
// and if it's too big, we just retry. This is still statistically O(1)
// since repeated probabilities less than one converge to zero. Hat-tip to
// a Google interview for teaching me this technique! ;)
@antishok
antishok / NODE_ENV production.md
Last active September 25, 2020 07:09
What does `NODE_ENV=production` do?
@possibilities
possibilities / meteor-async.md
Created August 23, 2012 22:53
Meteor Async Guide

From Meteor's documentation:

In Meteor, your server code runs in a single thread per request, not in the asynchronous callback style typical of Node. We find the linear execution model a better fit for the typical server code in a Meteor application.

This guide serves as a mini-tour of tools, trix and patterns that can be used to run async code in Meteor.

Basic async

Sometimes we need to run async code in Meteor.methods. For this we create a Future to block until the async code has finished. This pattern can be seen all over Meteor's own codebase: