Skip to content

Instantly share code, notes, and snippets.

@deadkarma
deadkarma / gist:1163716
Created August 22, 2011 21:53
rails log rotator
/var/apps/*/shared/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 777 root adm
}
@deadkarma
deadkarma / gist:1561438
Created January 4, 2012 18:51
get credit card type
function GetCreditCardTypeByNumber(ccnumber) {
var cc = (ccnumber + '').replace(/\s/g, ''); //remove space
if ((/^(34|37)/).test(cc) && cc.length == 15) {
return 'AMEX'; //AMEX begins with 34 or 37, and length is 15.
} else if ((/^(51|52|53|54|55)/).test(cc) && cc.length == 16) {
return 'MasterCard'; //MasterCard beigins with 51-55, and length is 16.
} else if ((/^(4)/).test(cc) && (cc.length == 13 || cc.length == 16)) {
return 'Visa'; //VISA begins with 4, and length is 13 or 16.
} else if ((/^(300|301|302|303|304|305|36|38)/).test(cc) && cc.length == 14) {
@deadkarma
deadkarma / gist:1989808
Created March 6, 2012 23:39 — forked from rails/gist:58761
Calculate time_ago_in_words for javascript
var DateHelper = {
// Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
// Ruby strftime: %b %d, %Y %H:%M:%S GMT
time_ago_in_words_with_parsing: function(from) {
var date = new Date;
date.setTime(Date.parse(from));
return this.time_ago_in_words(date);
},
// Takes a timestamp and converts it to a relative time
// DateHelper.time_ago_in_words(1331079503000)
@deadkarma
deadkarma / default.rb
Created March 20, 2012 20:45 — forked from emachnic/default.rb
Chef recipe to write redis.yml on Engine Yard Cloud
# ey-cloud-recipes/cookbooks/redis-yml/recipes/default.rb
if ['app_master', 'app'].include?(node[:instance_role])
redis_instance = node['utility_instances'].find { |instance| instance['name'] == 'redis' }
if redis_instance
node[:applications].each do |app, data|
template "/data/#{app}/shared/config/redis.yml"do
source 'redis.yml.erb'
owner node[:owner_name]
@deadkarma
deadkarma / gist:3068841
Created July 8, 2012 01:07 — forked from jordan-brough/gist:1684779
Decode Rails session cookie
cookie = 'XXX--YYY'
ActiveSupport::MessageVerifier.new(Rails.application.config.secret_token).verify(cookie)
@deadkarma
deadkarma / gist:3899580
Created October 16, 2012 14:24
Random String
rand(36**8).to_s(36)
@deadkarma
deadkarma / benhmark_strings.rb
Created May 7, 2013 14:11
String performance
require 'benchmark'
n = 1000000
Benchmark.bmbm do |x|
x.report("assign single") { n.times do; c = 'a string'; end}
x.report("assign double") { n.times do; c = "a string"; end}
x.report("concat single") { n.times do; 'a string ' + 'b string'; end}
x.report("concat double") { n.times do; "a string " + "b string"; end}
x.report("use << single") { n.times do; 'a string ' << 'b string'; end}
@deadkarma
deadkarma / benchmark_rescue.rb
Last active December 17, 2015 02:08
rescue benchmarks
require 'benchmark'
n = 100000
Benchmark.bmbm do |x|
x.report("rescue") { n.times { raise NoMethodError rescue nil } }
x.report('logic') { n.times { nil if Kernel.const_defined? "Foo"; } }
x.report('throw') { n.times { catch(:foo) { throw(:foo) } } }
end
@deadkarma
deadkarma / remove_merged_branches
Last active August 29, 2015 14:02
Remove all merged branches
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
@deadkarma
deadkarma / json-msgpack.rb
Last active August 29, 2015 14:11
Multi-JSON vs JSON vs Messagepack
require 'benchmark/ips'
require 'multi_json'
require 'json'
require 'msgpack'
hash = {
"site_config"=>
{
"created_at"=>"2014-03-24T19:58:25Z",
"data"=>"",