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:1033447
Created June 18, 2011 19:54
Escape single quotes to prepare for query
class String
def escape_single_quotes
self.gsub(/'/, "\\\\'")
end
end
array.inject{|sum,x| sum + x }
@trivektor
trivektor / gist:1049151
Created June 27, 2011 16:01
Skip callbacks
# Courtesy http://www.allerin.com/blog/?p=186
class ActiveRecord::Base
def self.skip_callback(callback, &block)
method = instance_method(callback)
remove_method(callback) if respond_to?(callback)
@trivektor
trivektor / gist:1053168
Created June 29, 2011 04:42
Convert number to currency
# Taken from http://codesnippets.joyent.com/posts/show/1812
# takes a number and options hash and outputs a string in any currency format
def currencify(number, options={})
# :currency_before => false puts the currency symbol after the number
# default format: $12,345,678.90
options = {:currency_symbol => "$", :delimiter => ",", :decimal_symbol => ".", :currency_before => true}.merge(options)
# split integer and fractional parts
int, frac = ("%.2f" % number).split('.')
@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