Skip to content

Instantly share code, notes, and snippets.

@asmuth
asmuth / gist:1064005
Created July 4, 2011 22:10
json<->marshal loading in ruby1.8<->ruby1.9
require 'json'
MY_JSON_FILE = File.open('big_json.json').read
MY_MARSHAL_FILE = File.open('big_json.dat').read
class FileLoader
def load_via_json
JSON.parse(MY_JSON_FILE)
end
@asmuth
asmuth / gist:1285498
Created October 13, 2011 20:54
if you're happy and you know it (ruby --debug)
puts "if you're happy and you know it, NoMethodError"
(nil.fnord rescue nil)
@asmuth
asmuth / mongoid_prefixable.rb
Created November 6, 2011 22:32
Dynamic (prefixed at runtime) collection names for mongoid models
# uber-hacky dynamic collection names for MongoID models:
#
# class MyModel
# include Mongoid::PrefixableDocument # ...instead of Mongoid::Document
# include Mongoid::Timestamps
#
# field :foobar, :type => Integer
#
# def my_method; 123; end
#
@asmuth
asmuth / gist:1373907
Created November 17, 2011 17:55
mongoid-bug?
require 'rubygems'
require 'rspec'
require 'mongoid'
describe "is this a bug?" do
Mongoid.configure do |config|
config.master = Mongo::Connection.new('localhost','27017').db('mongotest')
end
@asmuth
asmuth / gist:1557137
Created January 3, 2012 21:54
FnordMetric: setting user picture/name and unique gauges
# i hope this helps...
# to set the username and picture send these events to fm
# -> you don't need to define any event handlers
# -> the session token (_session) could be your unique session id
# or the unique user id (it's hashed)
# set the user name
{ "_type": "_set_name", "name": "Tingle Tangle Bob", "_session": "mysessiontoken" }
@asmuth
asmuth / node.rb
Created February 13, 2012 21:20
bp ultrasimple social graph (wip)
# the "abstract" node
class BPGraph::Node
# initialize an instance with id of the correct subclass from a node_key
def self.load(node_key)
end
def initialize(node_id)
@node_class ||= :node
@asmuth
asmuth / gist:1852251
Created February 17, 2012 09:47
[ruby/rake] deploy with foreman/deamonize (w/o capistrano)
namespace :tlnt do
desc "deploy to staging environment"
task :deploy_staging => :environment do
deploy_to_environment(:staging)
end
desc "restart staging environment"
task :restart_staging => :environment do
restart_environment(:staging)
@asmuth
asmuth / squirrelclock.c
Created February 17, 2012 22:45
squirrelclock! :)
#include <stdio.h>
#include <time.h>
void clearscreen(){
fputs("\033[H\033[2J", stdout);
}
int main(int argc, char* argv[]){
char *frame01 =
@asmuth
asmuth / simple_geohash.scala
Created February 21, 2012 21:20
simple geohash point on range to bitstream - my first scala script :)
def geohashify_n(n: Double, rLeft: Int, rRight: Int, depth: Int): String = {
val mid = ((rRight - rLeft) / 2) + rLeft
if (depth == 0)
return ""
if (n > mid)
"1" + geohashify_n(n, mid, rRight, depth - 1)
else
"0" + geohashify_n(n, rLeft, mid, depth - 1)
@asmuth
asmuth / roman_to_arabic.scala
Created February 24, 2012 01:33
Roman Numerals to Integer (learning scala) :)
import scala.collection.immutable.HashMap;
val tbl = HashMap[Char, Int](
'M' -> 1000,
'D' -> 500,
'C' -> 100,
'L' -> 50,
'X' -> 10,
'V' -> 5,
'I' -> 1