Skip to content

Instantly share code, notes, and snippets.

View u2's full-sized avatar
🐢
I may be slow to respond.

zhangyaning u2

🐢
I may be slow to respond.
View GitHub Profile
@u2
u2 / download.sh
Last active December 30, 2015 02:49 — forked from guzart/download
如何打包RailsAPI
#!/bin/bash
rm -rf api.rubyonrails.org/
wget -r -k -p http://api.rubyonrails.org/
rm rails_api.rar
rar a -r rails_api.rar api.rubyonrails.org/
@u2
u2 / compared.rb
Last active December 31, 2015 11:39
Nil compared with Numeric and String
# Allows `nil` to compare to String,Numeric,NilClass.So you can sort a array if it include nil.
#
# nil <=> '' # => 0
# nil <=> 'Tom' # => -1
# nil == '' # false
# For the NilClass the method == do not depond on the <=>.
#
# '' <=> nil # => 0
# 'Tom' <=> nil # => 1
# '' > nil # => false
@u2
u2 / alias_method_chain.rb
Last active December 31, 2015 14:19
alias_method_chain
# File activesupport/lib/active_support/core_ext/module/aliasing.rb, line 23
def alias_method_chain(target, feature)
# Strip out punctuation on predicates or bang methods since
# e.g. target?_without_feature is not a valid method name.
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
yield(aliased_target, punctuation) if block_given?
with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
without_method = "#{aliased_target}_without_#{feature}#{punctuation}"
@u2
u2 / once.rb
Last active December 31, 2015 14:19
Once
# once/once.rb: Written by Tadayoshi Funaba 1998,2000,2002,2006,2008
# $Id: once.rb,v 1.2 2008-11-12 19:58:01+09 tadf Exp $
module Once
def once(*ids)
for id in ids
module_eval <<-"end;"
alias_method :__#{id.object_id}__, :#{id.to_s}
def #{id.to_s}(*args, &block)
@u2
u2 / svn_resolved.sh
Last active December 31, 2015 14:59
svn resolved
svn st | grep ! | cut -c 9- | while read line;do svn resolved $line;done
@u2
u2 / list_alldir
Created December 17, 2013 12:49
Shell遍历文件夹并删除匹配文件
#!/bin/sh
list_alldir(){
for file2 in `ls -a $1`
do
if [ x"$file2" != x"." -a x"$file2" != x".." ];then
if [ -d "$1/$file2" ] && (echo $file2 | grep -qv '.svn');then
list_alldir "$1/$file2"
elif (echo $1/$file2 | egrep -q '(de|el|fr|it)\.yml$');then
echo "$1/$file2"
rm $1/$file2
@u2
u2 / url-encode.c
Created January 3, 2014 02:31 — forked from sudar/url-encode.c
int c;
char *hex = "0123456789abcdef";
while( (c = getchar()) != EOF ){
if( ('a' <= c && c <= 'z')
|| ('A' <= c && c <= 'Z')
|| ('0' <= c && c <= '9') ){
putchar(c);
} else {
putchar('%');
@u2
u2 / gist:8252372
Last active January 2, 2016 04:39
Rails Group日期(日/周/月/年)
def self.total_grouped_by(date_part)
case date_part
when 'day'
downloads = self.joins(:app_version).group("EXTRACT(YEAR from downloads.created_at),DATE(downloads.created_at),app_versions.name")
downloads = downloads.order("max(EXTRACT(YEAR from downloads.created_at)) desc,max(DATE(downloads.created_at)) desc,max(app_versions.name)")
downloads = downloads.select("EXTRACT(YEAR from downloads.created_at) as created_year,DATE(downloads.created_at) as created_part, app_versions.name as app_name, count(downloads.id) as total")
when 'week'
downloads = self.joins(:app_version).group("EXTRACT(ISOYEAR from downloads.created_at),EXTRACT(#{date_part} from downloads.created_at),app_versions.name")
downloads = downloads.order("max(EXTRACT(ISOYEAR from downloads.created_at)) desc,max(EXTRACT(#{date_part} from downloads.created_at)) desc,max(app_versions.name)")
downloads = downloads.select("EXTRACT(ISOYEAR from downloads.created_at) as created_year,EXTRACT(#{date_part} from
@u2
u2 / gist:8252386
Created January 4, 2014 06:27
Hash to url
require 'active_support/core_ext/object/to_param'
class Object
# Converts an object into a string suitable for use as a URL query string, using the given <tt>key</tt> as the
# param name.
#
# Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
def to_query(key)
require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
"#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
@u2
u2 / nagios
Created March 6, 2014 10:42
nagios3 + nginx
server {
listen 80;
server_name xxxxxxxxxxx;
access_log /var/log/nginx/nagios.access.log;
error_log /var/log/nginx/nagios.error.log info;
expires 31d;
root /usr/share/nagios3/htdocs;