Skip to content

Instantly share code, notes, and snippets.

View yaodong's full-sized avatar

Yaodong Zhao yaodong

View GitHub Profile

From WikiPedia:

A parity bit, or check bit is a bit added to the end of a string of binary code that indicates whether the number of bits in the string with the value one is even or odd. Parity bits are used as the simplest form of error detecting code.

There are two variants of parity bits: even parity bit and odd parity bit.

In the case of even parity, the number of bits whose value is 1 in a given set are counted. If that total is odd, the parity bit value is set to 1, making the total count of 1's in the set an even number. If the count of ones in a given set of bits is already even, the parity bit's value remains 0.

@yaodong
yaodong / gist:ddef36775e5608c6f1b1
Last active August 29, 2015 14:19
MySQL Case Expression vs Case Statement

From StackOverflow:

The statement does not evaluate to a value and can be used on its own, while the expression needs to be a part of an expression.

CASE expression:

SELECT CASE
    WHEN type = 1 THEN 'foo'
    WHEN type = 2 THEN 'bar'

From Best Practices for Speeding Up Your Web Site:

When the browser makes a request for a static image and sends cookies together with the request, the server doesn't have any use for those cookies. So they only create network traffic for no good reason. You should make sure static components are requested with cookie-free requests. Create a subdomain and host all your static components there.

If your domain is www.example.org, you can host your static components on static.example.org. However, if you've already set cookies on the top-level domain example.org as opposed to www.example.org, then all the requests to static.example.org will include those cookies. In this case, you can buy a whole new domain, host your static components there, and keep this domain cookie-free. Yahoo! uses yimg.com, YouTube uses ytimg.com, Amazon uses images-amazon.com and so on.

Another benefit of hosting static components on a cookie-free domain is that som

@yaodong
yaodong / gist:7a06597d30ceb1ad89c0
Last active August 29, 2015 14:19
Set Variable Default Value in PHP

In PHP, You can set default value for variables use ?: if value contains fasle value or undefined.

$value = null;
$value = $value ?: "default";
echo $value; // get "default"

Another way is:

instance of
┌──────┐
▼ │
┌─────────┐ instance of ┌────────┐ instance of ┌───────┐ │
│ "hello" │──────────────▶│ String │──────────────▶│ Class │──┘
└─────────┘ └────────┘ └───────┘
class Person
attr_accessor :name
def set_name
@name = 'joe'
end
def dump_by_varibale
puts @name
end
@yaodong
yaodong / gist:7eb60267dd8a742498a9
Created August 13, 2015 14:49
active record with ssl encryption
require 'openssl'
require 'digest/sha1'
require 'base64'
require 'json'
class Setting < ActiveRecord::Base
belongs_to :user, inverse_of: :settings
serialize :content, JSON
@yaodong
yaodong / gist:f133d542ab50166f0148
Created August 24, 2015 14:50
dnscrypt-proxy daemonize
sudo dnscrypt-proxy --resolver-name=opendns-port53 --local-address=127.0.0.1:5353 --logfile=/var/log/dnscrypt.log --pidfile=/var/run/dnscrypt.pid --daemonize
@yaodong
yaodong / nginx.conf
Created November 13, 2015 18:30 — forked from plentz/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
@yaodong
yaodong / github_api_move_file.rb
Last active November 24, 2015 16:56
Rename file use Github API
master_ref = client.ref(repo, 'heads/master')
last_commit = client.commit(repo, master_ref[:object][:sha])
last_tree = client.tree(repo, last_commit[:sha], recursive: true)
changed_tree = last_tree[:tree].map(&:to_hash).reject { |blob| blob[:type] == 'tree' }
changed_tree.each { |blob| blob[:path] = 'History.markdown-2' if blob[:path] == 'History.markdown' }
changed_tree.each { |blob| blob.delete(:url) && blob.delete(:size) }
new_tree = client.create_tree(repo, changed_tree)
new_commit = client.create_commit(repo, 'move file test', new_tree[:sha], last_commit[:sha])
client.update_ref(repo, 'heads/master', new_commit.sha)