Skip to content

Instantly share code, notes, and snippets.

View vincent178's full-sized avatar
💭
Hacking

Vincent Huang vincent178

💭
Hacking
View GitHub Profile
@vincent178
vincent178 / deploy.rb
Last active December 21, 2015 11:09
cap code snippets
# =========================
# For Bundler
# Cap will run "bundle --deploy" on the production server
# every time when you deploy
# ==========================
require 'bundler/capistrano'
set :application, "wechat_bot"
# ==========================
@vincent178
vincent178 / unicorn.rb
Last active December 21, 2015 11:09
unicorn code snippets
worker_processes 6
app_root = File.expand_path("../..", __FILE__)
working_directory app_root
# Listen on fs socket for better performance
listen "/tmp/unicorn.sock", :backlog => 64
listen 4096, :tcp_nopush => false
# Nuke workers after 30 seconds instead of 60 seconds (the default)
@vincent178
vincent178 / Geo location
Last active December 21, 2015 15:38
Google location to address
curl http://maps.google.com/maps/api/geocode/json?latlng=39.910093,116.403945&language=zh-CN&sensor=false
{
"results" : [
{
"address_components" : [
{
"long_name" : "128号",
"short_name" : "128号",
"types" : [ "street_number" ]
@vincent178
vincent178 / classes.rb
Last active December 21, 2015 16:09
Different class definitions in Ruby
class Logger
def self.add_logging
def log(msg)
STDERR.puts Time.now.strftime("%H:%M:%S: ") + "#{self} (#{msg})"
end
end
end
class Example < Logger
add_logging
@vincent178
vincent178 / Ruby.sublime-build
Created August 25, 2013 08:10
let sublime use rvm ruby; PATH: /Users/lhuang/Library/Application Support/Sublime Text 2/Packages/ruby
{
"env":{
"PATH":"${HOME}/.rvm/bin:${PATH}"
},
"cmd": ["rvm-auto-ruby", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.ruby"
}
@vincent178
vincent178 / module.rb
Created August 25, 2013 09:19
Add instance methods class methods in one module
module AttrLogger
def log(msg)
puts msg
end
module ClassMethods
def attr_logger(name)
attr_reader name
define_method("#{name}=") do |val|
puts "Assigning #{val.inspect} to #{name}"
@vincent178
vincent178 / module_methods_in_view_and_controller.rb
Last active December 21, 2015 20:29
module add helper methods, can use in view and controller
module Authentication
# Add helper method as soon as module included
def self.included(controller)
controller.send :helper_method, :current_user, :logged_in?, :redirect_to_target_or_default
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
@vincent178
vincent178 / ViewController.m
Created August 30, 2013 06:38
UITableViewDataSource Code Sample
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger result = 0;
if ([tableView isEqual:self.myTableView]) {
result = 3;
}
return result;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
@vincent178
vincent178 / UITableViewCellHeader.m
Created August 31, 2013 02:56
UITableViewCell Header Customize
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *result = nil;
if ([tableView isEqual:self.myTableView] && section == 0) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"Section 1 Header";
label.backgroundColor = [UIColor clearColor];
[label sizeToFit];