Skip to content

Instantly share code, notes, and snippets.

View trivektor's full-sized avatar
🏠
Working from home

Tri Vuong trivektor

🏠
Working from home
View GitHub Profile
@trivektor
trivektor / gist:1063487
Created July 4, 2011 15:25
Git commands
You can use
git add -u
To add the deleted files to the staging area, then commit them
git commit -m "Deleted files manually"
Delete a branch on Github
@trivektor
trivektor / gist:1076125
Created July 11, 2011 15:43
Convert seconds to hour:minute:seconds
def format_time_difference(start_time, end_time)
difference = end_time.to_i - start_time.to_i
hours = difference/3600.to_i
minutes = (difference/60 - hours * 60).to_i
seconds = (difference - (minutes * 60 + hours * 3600))
sprintf("%02d:%02d:%02d\n", hours, minutes, seconds)
end
@trivektor
trivektor / range_intersection.rb
Created July 13, 2011 19:31 — forked from senorprogrammer/range_intersection.rb
Range intersections in Ruby
#!/usr/bin/env ruby
# From my blog post at http://www.postal-code.com/binarycode/2009/06/06/better-range-intersection-in-ruby/
class Range
def intersection(other)
raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)
my_min, my_max = first, exclude_end? ? max : last
other_min, other_max = other.first, other.exclude_end? ? other.max : other.last
@trivektor
trivektor / gist:1085138
Created July 15, 2011 17:38
Convert UTC time to local time
Time.now.in_time_zone("Eastern Time (US & Canada)")
@trivektor
trivektor / gist:1087995
Created July 17, 2011 20:03
Handling exception in loop
for i in 1..5
begin
do_something_that_might_raise_exceptions(i)
rescue
next # do_something_* again, with the next i
end
end
@trivektor
trivektor / gist:1089840
Created July 18, 2011 15:20
How to calculate Easter Sunday
# created by Nathan Broadbent (http://madebynathan.com/2011/03/31/thanks-first-council-of-nicaea/)
def easter(year)
c=year/100
n=year-19*(year/19)
k=(c-17)/25
i=c-c/4-(c-k)/3+19*n+15
i-=30*(i/30)
i-=(i/28)*(1 -(i/28)*(29/(i+1))*((21-n)/11))
j=year+year/4+i+2-c+c/4
@trivektor
trivektor / gist:1106993
Created July 26, 2011 15:18
Use route helpers in model in Rails 3
Add
include Rails.application.routes.url_helpers
to model
@trivektor
trivektor / gist:1114674
Created July 29, 2011 20:32
Mysql 5.5 on Snow Leopard
Set up databases to run AS YOUR USER ACCOUNT with:
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
To set up base tables in another folder, or use a differnet user to run
mysqld, view the help for mysqld_install_db:
mysql_install_db --help
and view the MySQL documentation:
* http://dev.mysql.com/doc/refman/5.5/en/mysql-install-db.html
@trivektor
trivektor / gist:1126489
Created August 4, 2011 22:46
Set TextMate EDITOR environment variable
vi ~/.bash_profile
export EDITOR="mate"
source ~/.bash_profile
@trivektor
trivektor / gist:1143168
Created August 12, 2011 22:49
Restart apache on Mac OSX
sudo /usr/sbin/apachectl restart