Skip to content

Instantly share code, notes, and snippets.

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

Lucas Zhang marshluca

🏠
Working from home
View GitHub Profile
@marshluca
marshluca / zsh-bira-theme.diff
Created September 1, 2014 04:04
patch to bira.zsh-theme in oh my zsh
diff --git a/themes/bira.zsh-theme b/themes/bira.zsh-theme
index 2157204..003cb92 100644
--- a/themes/bira.zsh-theme
+++ b/themes/bira.zsh-theme
@@ -4,7 +4,7 @@ local return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})"
local user_host='%{$terminfo[bold]$fg[green]%}%n@%m%{$reset_color%}'
local current_dir='%{$terminfo[bold]$fg[blue]%} %~%{$reset_color%}'
local rvm_ruby=''
-if which rvm-prompt &> /dev/null; then
+if [ -e ~/.rvm/bin/rvm-prompt ]; then
@marshluca
marshluca / constant_vs_class_variable.rb
Last active August 29, 2015 14:05
benchmark on the speed of constant and class variable.
require 'benchmark'
LOCALE_ARRAY = (1..1000000).to_a
class MyClass
def self.vars
@@vars
end
def self.vars=(args)
@marshluca
marshluca / csrf_token_in_rails_ajax_request.markdown
Last active August 29, 2015 14:05
CSRF token in Rails Ajax request
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
end
$(document).ajaxSend(function(e, xhr, options) {
  var token = $("meta[name='csrf-token']").attr("content");
 xhr.setRequestHeader("X-CSRF-Token", token);
@marshluca
marshluca / extend_built_in_objects.coffee
Last active August 29, 2015 14:04
Extending Built-in Objects in CoffeeScript
# Use :: to assign your new function to the prototype of the object or class.
# Maintainable JavaScript: Don’t modify objects you don’t own; Extending built-in native objects. Evil or not?
# http://www.slideshare.net/nzakas/maintainable-javascript-1071179
unless String::trim then String::trim = -> @replace /^\s+|\s+$/g, ""
String::strip = -> if String::trim? then @trim() else @replace /^\s+|\s+$/g, ""
String::lstrip = -> @replace /^\s+/g, ""
String::rstrip = -> @replace /\s+$/g, ""
@marshluca
marshluca / sorting-algorithms.rb
Created July 21, 2014 05:43
经典排序算法资料及 ruby 实现 https://ruby-china.org/topics/20569
class Array
# 插入排序
def insert_sort!
(0...self.length).to_a.each do |j|
key = self[j]
i = j - 1;
while i >= 0 and self[i] > key
self[i+1] = self[i]
i = i-1
end
@marshluca
marshluca / dynamic_methods1.coffee
Created July 16, 2014 12:05
define dynamic methods in CoffeeScript
class MyClass
constructor: (@name) ->
for k, v of ['get', 'set']
console.log('creating method: ' + v)
MyClass::[v] = (args...) ->
method = v
console.log('executing method: ' + method)
o = new MyClass('dummy')
@marshluca
marshluca / mixin1.coffee
Last active August 29, 2015 14:04
Mixins for class in CoffeeScript
mixOf = (base, mixins...) ->
class Mixed extends base
for mixin in mixins by -1 # earlier mixins override later ones
for name, method of mixin::
Mixed::[name] = method
Mixed
class MixinClassOne
sayOne: -> "Hello One!"
@marshluca
marshluca / module_and_class.coffee
Last active August 29, 2015 14:03
define a module with `class` in coffeescript
class @MyModule
@init: ->
alert "Init MyModule"
return
MyModule.init()
class MyClass
constructor: (name) ->
@marshluca
marshluca / gist:2308868
Created April 5, 2012 07:57 — forked from jrochkind/gist:2161449
A Capistrano Rails Guide

A Capistrano Rails Guide

by Jonathan Rochkind, http://bibwild.wordpress.com

why cap?

Capistrano automates pushing out a new version of your application to a deployment location.

I've been writing and deploying Rails apps for a while, but I avoided using Capistrano until recently. I've got a pretty simple one-host deployment, and even though everyone said Capistrano was great, every time I tried to get started I just got snowed under not being able to figure out exactly what I wanted to do, and figured I wasn't having that much trouble doing it "manually".

From d3bb57d8e3974f2f6101184aafe4f4841b7af3f4 Mon Sep 17 00:00:00 2001
From: Lin Zhang <marshluca@gmail.com>
Date: Fri, 2 Mar 2012 11:36:06 +0800
Subject: [PATCH] defind method Wallpaper.upload
---
app/models/wallpaper.rb | 12 ++++++++++--
1 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/app/models/wallpaper.rb b/app/models/wallpaper.rb