Skip to content

Instantly share code, notes, and snippets.

@cwise
cwise / seeds_gen.rb
Created November 5, 2010 10:59
Generate a seeds.rb from existing data
# given a model (SomeModel), generate seeds.rb file from existing data in the database
elements=SomeModel.all.collect do |m|
"#{m.class.to_s}.create(#{m.attributes.select{|k, v| !["id", "created_at", "updated_at"].include?(k)}.collect{ |k, v| ":#{k} => '#{v.to_s.gsub("'", "\\\\'")}'" }.join(', ')})"
end
File.open('seeds.rb', 'w') {|f| elements.each{|element| f.puts(element)} }
@cwise
cwise / streak.rb
Created November 8, 2010 20:19
I want to refactor this method
def streak
return "-" if @played_games.empty?
games=@played_games
streak=0
game=games.pop
last_result=game.result_type(@team.id)
until game.nil? || game.result_type(@team.id)!=last_result do
game=games.pop
streak+=1
@cwise
cwise / calc.js
Created October 20, 2011 19:18
Calculate for recommendation
var calculate = function ($) {
var revenue=$('#revenue').val();
var margin = 0.3;
var profit1=(revenue * margin) - (revenue * 0.15);
var profit2=(revenue * margin) - (revenue * 0.05 + 100);
var profit3=(revenue * margin) - (revenue * 0.01 + 250);
$('#profit1').text(profit1);
$('#fee1').text(revenue * 0.15);
@cwise
cwise / gist:1353890
Created November 10, 2011 02:06
Ubuntu installation of RVM with openssl
rvm pkg install openssl
rvm remove 1.9.2
rvm install 1.9.2 --with-openssl-dir=$HOME/.rvm/usr
@cwise
cwise / gist:1353943
Created November 10, 2011 02:37
Ubuntu MySQL
The socket is in a weird spot on Ubuntu - add this symbolic link to put it where everyone expects:
ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
@cwise
cwise / gist:1353966
Created November 10, 2011 02:49
Ubuntu missing readline
You'll notice that readline is missing when attempting to run the console on Ubuntu.
sudo apt-get install install libreadline5-dev libncurses5-dev
rvm pkg install readline
cd $HOME/.rvm/src/ruby-1.9.2-p0/ext/readline
rvm remove 1.9.2
rvm install 1.9.2 --with-readline-dir=$rvm_path/usr
@cwise
cwise / learning_basic_programming.md
Created December 31, 2011 21:28 — forked from edward/learning_basic_programming.md
Tech-ing Up: Learning basic programming

Tech-ing Up: Learning basic programming

Coming from http://www.reddit.com/r/canada/comments/m1494/looking_for_a_developer_advocate_for_shopify/ or elsewhere? Awesome.

This is a guide for learning the bare minimum needed for effectively working as a junior programmer or developer advocate at most software companies.

If you haven’t ever programmed before, don’t sweat it. Those who would be good fits at this job are naturally attuned to this stuff and their innate curiosity makes it a cakewalk. If you’re finding that it’s a frustrating experience the entire way through, then you should consider doing something else for a little while before you come back to give it another shot.

A first stab at programming

@cwise
cwise / gist:1575082
Created January 7, 2012 15:46
EngineYard resque-web
require 'resque/server'
resque_constraint = lambda do |request|
request.env['warden'].authenticate? and request.env['warden'].user.admin? # or whatever manner your admin role can be determined
end
constraints resque_constraint do
mount Resque::Server.new, :at => "/resque"
end
@cwise
cwise / resque.sh
Created January 8, 2012 17:53
Rewritten /engineyard/bin/resque
#!/bin/sh
#
# This script starts and stops the Resque daemon
# This script belongs in /engineyard/bin/resque
#
PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
CURDIR=`pwd`
usage() {
@cwise
cwise / default.rb
Created April 20, 2012 23:43
resque cookbook
#
# Cookbook Name:: resque
# Recipe:: default
#
if ['solo', 'util'].include?(node[:instance_role])
execute "install resque gem" do
command "gem install resque redis redis-namespace yajl-ruby -r"
not_if { "gem list | grep resque" }
end