Skip to content

Instantly share code, notes, and snippets.

View bachue's full-sized avatar

Bachue Zhou bachue

View GitHub Profile
@dlueth
dlueth / mdnsresponder.sh
Last active April 16, 2022 03:02
Replace Yosemite's DNS discoveryd with the former mDNSResponder.
#!/bin/bash
if [ ! $(whoami) = 'root' ]; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
spinner()
{
local pid=$!
@wsargent
wsargent / docker_cheat.md
Last active June 29, 2024 19:32
Docker cheat sheet
@NalaGinrut
NalaGinrut / kmp.scm
Last active December 19, 2015 08:29
A KMP string match algorithm implementation
(use-modules (srfi srfi-1) (ice-9 receiver))
(define (gen-xfix-list str generator)
(let lp((ret '()) (n (1- (string-length str))))
(if (zero? n) ret (lp (cons (generator str n) ret) (1- n)))))
(define (gen-prefix-list str)
(gen-xfix-list str (lambda (str n) (substring str 0 n))))
(define (gen-suffix-list str)
@nightire
nightire / Changes in Rails 4_1.md
Last active May 11, 2022 04:50
拥抱 Rails 4 —— 详述 Rails 4 的新变化

Routes

小心地使用 Match(Rails 3 已实现)

Rails 3 提供了 match 方法供我们自定义 routes,然而我们要小心使用它以避免“跨站脚本攻击”(XSS Attack)。比如像这样的 routes:

注:(r3 代表 Rails 3,r4 代表 Rails 4)

# routes.rb
@vanstee
vanstee / authorized_keys_command.rb
Created February 23, 2013 08:30
Ruby script for AuthorizedKeysCommand that grabs public keys off github with the login
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'pry'
class User
attr_accessor :login
@piscisaureus
piscisaureus / pr.md
Created August 13, 2012 16:12
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = [email protected]:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

Fibur

Fibur is a library that allows concurrency during Ruby I/O operations without needing to make use of callback systems. Traditionally in Ruby, to achieve concurrency during blocking I/O operations, programmers would make use of Fibers and callbacks. Fibur eliminates the need for wrapping your I/O calls with Fibers and a callback. It allows you to write your blocking I/O calls the way you normally would, and still have concurrent execution during those I/O calls.

Example

Say you have a method that fetches data from a network resource:

@bkimble
bkimble / gist:1365005
Last active August 22, 2024 14:21
List local memcached keys using Ruby
#!/usr/bin/env ruby
# List all keys stored in memcache.
# Credit to Graham King at http://www.darkcoding.net/software/memcached-list-all-keys/ for the original article on how to get the data from memcache in the first place.
require 'net/telnet'
headings = %w(id expires bytes cache_key)
rows = []
@tmm1
tmm1 / a.txt
Created March 4, 2009 02:27
simple heap dumper for ruby 1.8
$ ./miniruby -e 'class T; end; a = 1..1; b = "hi"; c = []; d = Hash.new; e = T.new; p GC.dump_heap'
0x00154750 allocated @ -e:1 is an OBJECT of type: T
0x0015476c allocated @ -e:1 is a HASH which has data
0x00154788 allocated @ -e:1 is an ARRAY of len: 0.
0x001547c0 allocated @ -e:1 is a STRING (ELTS_SHARED) which has len: 2 and val: hi
0x001547dc allocated @ -e:1 is a STRING which has len: 1 and val: T
0x001547f8 allocated @ -e:1 which is a CLASS no name - maybe anon class?
0x00154814 allocated @ -e:1 which is a CLASS named: T inherits from Object
0x00154a98 allocated @ -e:1 is a STRING which has len: 2 and val: hi
0x00154b40 allocated @ -e:1 is an OBJECT of type: Range