Skip to content

Instantly share code, notes, and snippets.

View khacanh's full-sized avatar
😀

khacanh khacanh

😀
  • Ho Chi Minh city
View GitHub Profile
@khacanh
khacanh / NSArray+Ext.m
Created May 28, 2013 05:39
Some extensions to obj-c NSArray class
@implementation NSArray(Ext)
-(NSArray*)subarrayWithSize:(int)returnSize
{
int count = self.count;
if (returnSize >= 0 && returnSize <= count)
{
NSMutableArray *result = [[NSMutableArray alloc] init];
NSMutableArray *indexes = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < returnSize; i++)
@khacanh
khacanh / NSDictionary+Ext.m
Created May 28, 2013 07:43
Extensions for NSDictionary
#import "NSDictionary+Ext.h"
@implementation NSDictionary(Ext)
-(float)floatForKey:(NSString*)key
{
return [[self objectForKey:key] floatValue];
}
@khacanh
khacanh / my_attributes.rb
Created June 9, 2013 08:11
My simple and erring dynamic method generation
module MyAttributes
def self.included base
base.extend ClassMethods
base.send :include, InstanceMethods
end
module ClassMethods
def has_attributes attrs
attrs.each do |attr|
attr = attr.to_s
@khacanh
khacanh / dbConfig.js
Created June 18, 2013 04:43
Simple DB interface for phonegap / sqlite for app SpeakEnglish
var SpeakEnglish = {
db: {
name: 'speakEnglishDB',
displayName: 'Speak English DB',
version: '1.0',
size: 1000000,
categories: {
name: 'categories',
columns: ['name']
},
@khacanh
khacanh / .gitignore
Created June 23, 2013 07:52
# .gitignore for eclipse android project, source: http://stackoverflow.com/a/4130635/1039194
# .gitignore for eclipse android project, source: http://stackoverflow.com/a/4130635/1039194
# built application files
*.apk
*.ap_
# files for the dex VM
*.dex
# Java class files
*.class
@khacanh
khacanh / js+ext.js
Last active December 18, 2015 21:29
Some js extensions
String.prototype.capitalize = function() {
return this.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
};
String.prototype.contains = function(subStr) {
return this.indexOf(subStr) !== -1;
};
String.prototype.containsWord = function(word) {
var expStr = Mustache.render(" {{word}} |^{{word}} | {{word}}$|^{{word}}$", {word: word});
@khacanh
khacanh / divisors.rb
Created December 1, 2013 15:51
Divisors
class Fixnum
def divisors
ceil = Math.sqrt(self).ceil
(1..ceil).select {|i| self % i == 0 }
end
end
function createCookie(value) {
var name = 'sessionid';
var days = 3;
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+";";
pg_restore --host localhost --port 5432 --username "postgres" --dbname "my_dev" --no-password --verbose "server.dump"
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_relation_size(C.oid) DESC
LIMIT 20;
Example output (from a database created with pgbench, scale=25):