Skip to content

Instantly share code, notes, and snippets.

#!/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
$ 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}}'
# 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 October 11, 2010 14:26
Myself versus consultants #1
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 October 13, 2010 10:30
Myself versus consultants #2
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 December 6, 2010 13:16
Myself versus consultants #3
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")
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 February 15, 2011 15:08
optimizing some calendar logic
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 13:58
traversing params
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
@leikind
leikind / wice_grid.js
Created July 6, 2011 12:54
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();