Skip to content

Instantly share code, notes, and snippets.

# Created by Nando Vieira
# Usage: compound_interest(:amount => 1000, :interest => 0.1, :times => 5)
def compound_interest(options={})
options[:amount] * ((1 + options[:interest]) ** options[:times])
end
@fnando
fnando / gist:504060
Created August 2, 2010 03:12
Resize multiple apps
repeat with theApp in {"iTerm", "TextMate", "Safari"}
tell application theApp
activate
set the bounds of the first window to {0, 540, 960, 1080}
end tell
end repeat
// Load dependencies
var app = require("./lib/server").createServer(),
sys = require("sys");
// Start app server
app.listen(3002);
app.get("/:name", function(req, res){
res.render("Hello " + req.params.name);
});
raise "Check #{__FILE__} and http://bit.ly/9IK52W to see if this hack is still needed." if Rails.version > "3.0.0"
class ActionController::Base
private
alias :_compute_redirect_to_location_original :_compute_redirect_to_location
def _compute_redirect_to_location(options)
case options
when Proc
_compute_redirect_to_location instance_eval(&options)
class Achievement
class << self
attr_accessor :all
end
self.all = []
attr_accessor :badge
def initialize(badge)
@badge = badge
@fnando
fnando / database_cleaner.rb
Created October 15, 2010 21:46
A simple DatabaseCleaner replacement that only support TRUNCATE and ActiveRecord. Had a hard time using original DatabaseCleaner with Rails 3.
module DatabaseCleaner
def self.clean
Array.new.tap do |cache|
ActiveRecord::Base.descendants.each do |model|
next if cache.include?(model.table_name)
cache << model.table_name
table_name = ActiveRecord::Base.connection.quote_table_name(model.table_name)
ActiveRecord::Base.connection.execute "TRUNCATE TABLE #{table_name}"
end
end
@fnando
fnando / token.rb
Created November 10, 2010 02:10
Generate readable tokens
class Token
VOWELS = %w[a e i o u]
CONSONANTS = ("a".."z").to_a - VOWELS
NUMBERS = (0..9).to_a
def self.readable(size = 12)
String.new.tap do |s|
half = size / 2
1.upto(size) do |i|
@fnando
fnando / gist:715765
Created November 25, 2010 18:27
jQuery from Google CDN with local fallback for Rails apps
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery === "undefined") {
document.write(unescape("%3Cscript src='<%= compute_public_path("jquery-1.4.4.min.js", "javascripts") %>' type='text/javascript'%3E%3C/script%3E"));
};
</script>
@fnando
fnando / gist:721070
Created November 30, 2010 02:58
Bootstrapping RubyGems with Thor
require "active_support/all"
class Newgem < Thor::Group
include Thor::Actions
argument :path
class_option :rspec, :type => :boolean, :group => :test_framework, :desc => "Use RSpec as testing framework"
class_option :test_unit, :type => :boolean, :group => :test_framework, :desc => "Use Test::Unit as testing framework"
def prepare_for_invocation
@fnando
fnando / unindent.rb
Created November 30, 2010 19:57
Remove indentation from all lines, based on the first one.
class String
def unindent
_, spaces = *match(/^([\t ]+)/)
spaces ? gsub(%r[^#{spaces}]ms, "") : self
end
end
html = <<-CODE.unindent
THIS IS JUST A TEST
AND THIS A TEXT SNIPPET THAT SHOULD