Skip to content

Instantly share code, notes, and snippets.

View gist:415983
#!/bin/env ruby
# Quick and dirty script to merge N small mp3 files in the current directory into files with duration ~30 minutes
# The power of Unix + the power of Ruby :)
# Question: how to reduce the number of used tools (mplayer, sox, and lame) ?
i = 1
threshold = 60 * 30
View gist:443390
$ cat in.txt
1000,1030
1000,1040
1040,1000
3000,3010
3010,3000
# with ruby
cat in.txt | ruby -n -e 'BEGIN{@a = {}}; x,y = $_.chomp.split(","); if y && x && ! y.empty? then @a[x] = y; puts $_ ; end ; END{@a.keys.each{|k| puts k + "," + k}}'
View gist:576977
# export PS1="\u:\w$ "
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
# git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
# no highlight
# export PS1="\u:\w \$(parse_git_branch)$ "
# git branch in bold
@leikind
leikind / gist:620590
Created Oct 11, 2010
Myself versus consultants #1
View gist:620590
real_time1 = Benchmark.measure { # Written by consultants
# Perform a query for every zone getting companies in that zone,
# initialize all active records, merge results into one list, remove duplicates
@advertisers = @zones.map{ |z| z.companies.available }.flatten.uniq
}.real
real_time2 = Benchmark.measure { # Written by me
@_advertisers = Company.available.find(:all, :include => :zones, :conditions => ['zones.id in (?)', @zones])
}.real
@leikind
leikind / gist:623802
Created Oct 13, 2010
Myself versus consultants #2
View gist:623802
def create
Order.transaction do
ArticlePurchase.transaction do
@order = create_order # can go wrong due to programmers' mistakes
@order.purchase # can go wrong due to programmers' mistakes
flash[:notice] = :order_successfully_created.l
end
end
redirect_to account_root_path
rescue # Intercepting ALL errors, so the user is
@leikind
leikind / 3.rb
Created Dec 6, 2010
Myself versus consultants #3
View 3.rb
elsif @advertiser=Company.find(params[:filter][:advertiser_id])
@point_transactions = current_user.point_transactions_as_target.find_all_by_keyword("AD_REDEEMING") +
current_user.point_transactions_as_source.find_all_by_keyword("ARTICLE_PURCHASE") +
current_user.point_transactions_as_target.find_all_by_keyword("WHITE_TRANSACTION") +
current_user.point_transactions_as_source.find_all_by_keyword("WHITE_TRANSACTION") +
current_user.point_transactions_as_target.find_all_by_keyword("CARD_INFO") +
current_user.point_transactions_as_source.find_all_by_keyword("CARD_INFO") +
current_user.point_transactions_as_target.find_all_by_keyword("CARD_POSITIVE") +
current_user.point_transactions_as_source.find_all_by_keyword("CARD_POSITIVE")
View add_index.rb
desc 'generate add_index declarations for all foreign key fields'
task :indexes => :environment do
files = nil
Dir.chdir(File.join(RAILS_ROOT, "app/models" )) do
files = Dir["*.rb"]
end
models = []
files.each do |m|
@leikind
leikind / gist:827624
Created Feb 15, 2011
optimizing some calendar logic
View gist:827624
require 'rubygems'
require 'activesupport'
class Contract
attr_reader :start_date, :renew_date
def initialize(start_date, renew_date)
@start_date, @renew_date = start_date, renew_date
end
@leikind
leikind / gist:986735
Created May 23, 2011
traversing params
View gist:986735
class HashWithIndifferentAccess
def get_to(*keys)
subhash = self
keys.each do |k|
subhash = subhash.try(:[], k)
return nil if subhash.nil?
end
if block_given? && subhash.any?
yield subhash
else
View wice_grid.js
function WiceGridProcessor(name, base_request_for_filter, base_link_for_show_all_records,
link_for_export, parameter_name_for_query_loading, parameter_name_for_focus, environment){
this.checkIfJsFrameworkIsLoaded = function(){
if (! jQuery){
alert("jQuery not loaded, WiceGrid cannot proceed!")
}
}
this.checkIfJsFrameworkIsLoaded();