Skip to content

Instantly share code, notes, and snippets.

View shunchu's full-sized avatar

Shun Chu shunchu

View GitHub Profile
@shunchu
shunchu / Auto focus first form field
Created February 25, 2012 01:15
Auto focus first form field with a set of matching criteria
// focus first form field element matching these criteria
$(':input:visible:enabled:not(.no-focus,
select, input[readonly=readonly],
input[type=checkbox],
input[type=radio],
input[type=submit]):first').focus();
@shunchu
shunchu / Javascript callbacks with args
Created March 19, 2012 18:02
Passing arguments in JavaScript callbacks
http://stackoverflow.com/questions/3458553/javascript-passing-parameters-to-a-callback-function
function tryMe (param1, param2) {
    alert(param1 + " and " + param2);
}
function callbackTester (callback) {
    callback (arguments[1], arguments[2]);
}
callbackTester (tryMe, "hello", "goodbye");
@shunchu
shunchu / dynamic_proxy.rb
Created June 5, 2012 07:17 — forked from nusco/dynamic_proxy.rb
Spell: Dynamic Proxy
# ====================
# Spell: Dynamic Proxy
# ====================
# Forward to another object any messages that don’t match a method.
class MyDynamicProxy
def initialize(target)
@target = target
end
@shunchu
shunchu / box-sizing-with-100percent.css
Created June 19, 2012 20:32
Define "100% - 50px" in css
/* Have padding and border that cut into the element's width instead expanding it. */
textarea {
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
}
/* reference: http://css-tricks.com/box-sizing/ */
@shunchu
shunchu / pg.rake
Created July 8, 2012 10:25 — forked from royratcliffe/pg.rake
Rake task to dump PostgreSQL structure using a compatible pg_dump
namespace :pg do
namespace :structure do
# Does exactly the same as db:structure:dump but with one important
# difference. It tries to use a compatible version of pg_dump. If you see
# the following error message, use pg:structure:dump instead.
#
# pg_dump: server version: 9.1.0; pg_dump version: 9.0.4
# pg_dump: aborting because of server version mismatch
# rake aborted!
# Error dumping database
@shunchu
shunchu / convert-seconds-into-hh-mm-ss-in-ruby.rb
Created July 25, 2012 07:58
Convert seconds into HH:MM:SS in Ruby
t = 236 # seconds
Time.at(t).utc.strftime("%H:%M:%S")
=> "00:03:56"
# Reference
# http://stackoverflow.com/questions/3963930/ruby-rails-how-to-convert-seconds-to-time
@shunchu
shunchu / video-thumbnails-generator.sh
Created November 15, 2012 18:00
Batch generate thumbnails from mp4 files using ffmpg
#!/bin/bash
# setup
FILES=name_of_file_to_match_*.mp4
SEEK_POINT=00:00:30
IMG_FORMAT=png
FRAME_SIZE=150X100
DEST=thumbnails
for f in $FILES
@shunchu
shunchu / no-ruby-gem-docs
Created May 10, 2013 18:01
Don't install Ruby gem docs while installing gems
echo "gem: --no-ri --no-rdoc" >> ~/.gemrc
@shunchu
shunchu / ways_to_create_ruby_singleton_methods.rb
Last active December 20, 2015 01:18
Ways to create Singleton Methods in Ruby
# Reference: http://madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html
class Dog
def self.closest_relative
"wolf"
end
end
class Dog
class << self
@shunchu
shunchu / go-to-git-branch-on-github-repo.sh
Last active August 29, 2015 14:05
Go to git branch on Github repo
# copy/paste in ~/.bash_profile and invoke it by typing "gh" in commandline
# Opens the github page for the current git repo/branch in your browser
function gh() {
giturl=$(git config --get remote.origin.url)
if [ "$giturl" == "" ]
then
echo "Not a git repository or no remote.origin.url set"
exit 1;