Skip to content

Instantly share code, notes, and snippets.

require 'mongoid'
require 'minitest'
require 'minitest/autorun'
class Foo
include Mongoid::Document
field :x, type: Integer
field :y, type: Integer
end
@kizzx2
kizzx2 / foo.js
Last active December 20, 2015 00:59
var docs = [];
for(var i = 0; i < 20000; i++) {
docs.push({rnd: _rand(), t: "2013-06-23", N:7*i, A:1, V:0, n:"1234"});
}
docs.sort(function(a, b) {
return a.rnd - b.rnd;
});
var t0 = Date.now();
db.foos.insert(docs);
@kizzx2
kizzx2 / post.rb
Created February 6, 2013 14:53
A clean and elegant approach to partial object validation with Rails + Wicked wizards (store partial object in database)
class Post < ActiveRecord::Base
attr_accessible :body, :price, :title
validates_presence_of :title
validates_length_of :title, minimum: 10
validates_presence_of :body
validates_numericality_of :price, greater_than: 0
end
@kizzx2
kizzx2 / mark-email.rb
Last active December 9, 2015 21:58
Markdown emails with syntax highlighting on each <pre> block
#!/usr/bin/env ruby
# encoding: UTF-8
require 'nokogiri'
require 'open3'
html = Open3.popen2('redcarpet') do |stdin, stdout|
stdin.write(STDIN.read)
stdin.close
@kizzx2
kizzx2 / attach.rb
Created November 1, 2012 12:47
Attach debugger to arbitrary Ruby process. Almost working
require 'tempfile'
require 'debugger'
DEBUGGER_MONKEY_PATCH = <<'EOF'
module Debugger
class << self
def stop_remote
return unless @thread || @control_thread
@stopped = true
@kizzx2
kizzx2 / r-test.vim
Last active October 8, 2015 07:48
Vim function to run individual test function in Ruby MiniTest
function! Rtest()
ruby <<END
line = $curbuf.line
fn = nil
if line =~ /^\s*test\s+["']([^"']+)["']\s+do/
fn = "test_#{$1.gsub(/ /, '_')}"
elsif line =~ /^\s*def\s+(test_.+)/
fn = $1
end
@kizzx2
kizzx2 / UBlog.html
Created February 26, 2012 13:58
Minimal single-file Backbone.js demo
<!DOCTYPE html>
<html>
<head>
<script src="require.js"></script>
<script>
require(['jquery', 'underscore', 'backbone'],
function($)
{
var UBlog = {};
@kizzx2
kizzx2 / Rakefile
Created December 29, 2011 15:28
Use premake4 to generate iOS .xcodeproj files
# Usage:
# In premake4.lua use StaticLib for libraries; ConsoleApp for applications.
# Then run `rake xcode && rake xcodebuild`
ENV_DEFAULTS = {
'CONFIGURATION' => "Debug",
'SDK' => "iphonesimulator",
}
SDK_TO_ARCH = {
@kizzx2
kizzx2 / Worddy.hs
Last active September 27, 2015 08:17
Solution to Word Numbers (Haskell)
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NamedFieldPuns #-}
-- Chris Yuen <chris@kizzx2.com>
-- ITA Software's "Word Numbers" puzzle (http://www.itasoftware.com/careers/puzzle_archive.html)
import Data.Function
import Data.List
-- | For this problem we'll trie like structure. An "umbrella" value is
@kizzx2
kizzx2 / Solitaire.hs
Created September 4, 2011 09:00
Solitaire Cipher in Haskell
-- Solitaire Cipher
-- http://www.rubyquiz.com/quiz1.html
module Solitaire where
import Data.Sequence ((><), (|>), (<|))
import qualified Data.Sequence as S
import Debug.Trace
import Control.Arrow