Skip to content

Instantly share code, notes, and snippets.

@davidrenne
davidrenne / gist:4288545
Created December 14, 2012 20:51
load YML with ERB parser.
YAML.load(ERB.new(File.new("#{Rails.root}/config/xxxx.yml").read).result)[ENV['RAILS_ENV']]
@davidrenne
davidrenne / new gem.sh
Created December 12, 2012 17:07
setting up a new gem in bash
CAMELCASE=WidgetList
GEMNAME=widget_list
rvm gemset create $GEMNAME
rvm gemset use $GEMNAME
gem install bundler
bundle gem $GEMNAME
#chown wcapp:wcapp -R $GEMNAME/*
mkdir -p vendor/assets/{images,javascripts,stylesheets}
@davidrenne
davidrenne / gist:4207799
Created December 4, 2012 19:31
Parse Command Line Arguments in ruby
if $0 == __FILE__
ARGV.each{|arg|
case arg
when /\A--ruby=(.+)/
$ruby_program = $1
when /\A--matzruby=(.+)/
$matzruby_program = $1
when /\A--opts=(.+)/
$opts = $1
when /\A(-r|--only-ruby)\z/
birds = ["Golden Eagle", "Gyrfalcon", "American Robin",
            "Mountain BlueBird", "Mountain-Hawk Eagle"]
grouped_by_first_letter = birds.group_by { |s| s[0] }
module Enumerable
# clumps adjacent elements together
# >> [2,2,2,3,3,4,2,2,1].cluster{|x| x}
# => [[2, 2, 2], [3, 3], [4], [2, 2], [1]]
def cluster
cluster = []
each do |element|
if cluster.last && yield(cluster.last.last) == yield(element)
cluster.last << element
else
@davidrenne
davidrenne / gist:3420197
Created August 21, 2012 23:09
Import Any CSV into mySQL queries
<?php
$file = 'C:\\data.csv';
$fields = array();
$sqlOutput = '';
if($f = fopen($file, 'r'))
{
$fields = fgetcsv($f);
fclose($f);
if (!empty($fields))
@davidrenne
davidrenne / cron tab delete old
Created August 6, 2012 12:48
find files with extension sql who are older than 1 day and delete them
0 1 * * sun find /tmp -type f -a -mtime +1 -name "*.sql" -exec rm \{\} \;
@davidrenne
davidrenne / gist:3230999
Created August 1, 2012 21:40
json_encode_non_buggy
function json_encode_non_buggy($array, $return=false)
{
//fixes cases where json_decode will crap out.
foreach ($array as $k=>$v)
{
$array[$k] = str_replace(array("\t",'"'),array(" ","&quot;"), utf8_encode($v));
}
if($return)
{
@davidrenne
davidrenne / gist:3143568
Created July 19, 2012 12:42
Import Excel to Mysql using autoit.
;copy the top header columns. save as C:\import.csv and it will build the needed SQL to import
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $cTooltipClick
$clipBoardContents = ClipGet()
$array1 = StringExplode($clipBoardContents, @TAB, 0)
$cols = ""
$tableDef = "DROP TABLE IF EXISTS `development`.`import_tmp`; " & @CRLF & @CRLF & "CREATE TABLE `development`.`import_tmp` ("
@davidrenne
davidrenne / gist:2594907
Created May 4, 2012 13:50
AutoLoadMinifier
<?php
//It turns out, that it takes more memory and same amount of time to have all your includes in one file. I thought that all the file I/O would be less with one huge compiled include file. This class will combine all your includes into one file.
//it could be useful to try to see all your includes top down. But over all is not meant for production
//Ended up using spl_autoload_register which took all my production includes from 40MB to 10MB and increased speed by 1 to 3 seconds
/*
implementation