Skip to content

Instantly share code, notes, and snippets.

View caok's full-sized avatar

Clark caok

View GitHub Profile
@caok
caok / gist:6067486
Created July 24, 2013 01:30
ruby 匹配 @ 艾特用户名的方法
# coding: utf-8
#正则用于匹配用户名
str = "@wikimo @中國 @水手 测试看看"
#{2,20}字符长度至少2个,不多于20个,以下任意方式匹配
#arr = str.scan(/@([一-龠\w]{2,20}\s)/u).flatten
arr = str.scan(/@([\p{Han}+\w]{2,20}\s)/u).flatten
前端:https://github.com/ichord/jquery-atwho-rails
@caok
caok / 实现div的上下左右都居中
Created November 19, 2013 08:32
其实解决的思路是这样的:首们需要position:absolute;绝对定位。而层的定位点,使用外补丁margin负值的方法。负值的大小为层自身宽度高度除以二。
<style type="text/css">
<!--
div {
position:absolute;
top:50%;
left:50%;
margin:-150px 0 0 -200px;
width:400px;
height:300px;
border:1px solid #008800;
#!/usr/bin/env bash
# Pre-requisites
sudo apt-get -y update
sudo apt-get -y install git-core curl autoconf automake openssl vim build-essential libc6-dev libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev mysql-server libmysqlclient-dev ncurses-dev libtool bison libxslt1-dev libxml2-dev
# rbenv
curl https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
app/views/layouts/_async_javascript.html.erb
<script type="text/javascript">
(function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = '<%= j javascript_path(path) %>';
var other = document.getElementsByTagName('script')[0];
other.parentNode.insertBefore(script, other);
@caok
caok / gist:8018311
Created December 18, 2013 06:55
图片不存在时,显示一个默认的图片
<img src="abc.JPG" onerror="this.src='default.JPG'" />
或者
<script type="text/javascript">
t = document.getElementsByClassName("defaultImg");
for(i = 0; i < t.length; i++){
t.item(i).onerror = function(){
this.src = "test.gif"
@caok
caok / gist:8049246
Last active December 31, 2015 21:49
Action Pack Variants
class ApplicationController < ActionController::Base
before_action :detect_device_variant
private
def detect_device_variant
case request.user_agent
when /iPad/i
request.variant = :tablet
when /iPhone/i
@caok
caok / gist:8108019
Created December 24, 2013 02:32
jQuery 屏蔽单个元素使用户无法点击
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
// 下面的插件部分建议放在js文件中, 方便调用
//-------------- 插件 begin ------------------
(function ($) {
@caok
caok / format_datetime.js
Last active January 2, 2016 01:39
时间对象的格式化
Date.prototype.format = function (format) {
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
var o = {
"M+" : this.getMonth() + 1, // month
"d+" : this.getDate(), // day
"h+" : this.getHours(), // hour
"m+" : this.getMinutes(), // minute
"s+" : this.getSeconds(), // second
@caok
caok / haiku.rb
Last active January 2, 2016 07:19
Active Record Reputation System
-- <%= haiku.user.name %>
| <%= pluralize haiku.votes, "vote" %>
<% if current_user && current_user.can_vote_for?(haiku) %>
| <%= link_to "up", vote_haiku_path(haiku, value: 1), method: "post" %>
| <%= link_to "down", vote_haiku_path(haiku, value: -1), method: "post" %>
<% end %>
@caok
caok / gist:8466786
Last active January 3, 2016 12:59
使用纯CSS3来生成家谱(family tree)
<!--
We will create a family tree using just CSS(3)
The markup will be simple nested lists
-->
<div class="tree">
<ul>
<li>
<a href="#">Parent</a>
<ul>
<li>