Skip to content

Instantly share code, notes, and snippets.

View YanhaoYang's full-sized avatar

Yanhao Yang YanhaoYang

  • Berlin, Germany
View GitHub Profile
@YanhaoYang
YanhaoYang / redefine-a-method-by-opening-the-class.rb
Last active May 21, 2016 08:44
Redefine a method by opening the class
class A
def hi
puts 'hi'
end
end
a = A.new
a.hi
# => "hi"
@YanhaoYang
YanhaoYang / redefine-a-method-in-a-module.rb
Created May 21, 2016 08:42
Redefine a method in a module
module M1
def hi
puts "hi in M1"
end
end
class A
include M1
def ha
@YanhaoYang
YanhaoYang / monkey-patch-by-alias_method.rb
Last active May 21, 2016 09:21
Monkey patch by alias_method
class A
def hi
puts "hi"
end
end
a = A.new
a.hi
# => hi
@YanhaoYang
YanhaoYang / monkey-patch-by-prepend.rb
Created May 21, 2016 13:04
Monkey patch by prepend
class A
def hi
puts 'hi'
end
end
a = A.new
a.hi
# => "hi"
@YanhaoYang
YanhaoYang / close_channel.go
Created February 2, 2017 07:40
Ensure channel is closed, but not close it twice
done := make(chan struct{})
defer func(c chan struct{}) {
select {
case <-c:
default:
close(c)
}
}(done)
@YanhaoYang
YanhaoYang / nested-structs-to-json.go
Created February 6, 2017 08:11
Golang: json.Marshal for nested structs
package main
import (
"fmt"
"encoding/json"
)
type A struct {
A string
}
@YanhaoYang
YanhaoYang / closable.go
Created February 9, 2017 07:46
Solution: error return value not checked (defer response.Body.Close())
// Closable wraps Close() method
type Closable interface {
Close() error
}
func closeIt(obj Closable) {
err := obj.Close()
if err != nil {
log.Fatal(err)
}
@YanhaoYang
YanhaoYang / callerinfo.go
Created February 15, 2017 06:37
Print caller info
package main
import (
"fmt"
"path"
"runtime"
)
func callerInfo() string {
function, file, line, _ := runtime.Caller(0)
@YanhaoYang
YanhaoYang / Static Website Hosting.md
Created February 28, 2017 02:57
AWS S3 Static Website Hosting with redirection

Redirection Rules

Suppose there are two buckets: one-bucket, another-bucket.

Show content in another bucket when the file is missing in one bucket:

  <RoutingRules>
    <RoutingRule>
    <Condition>
      <HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals >
@YanhaoYang
YanhaoYang / 1. alloc.go
Last active March 15, 2017 07:58
Golang memory allocation and GC -- array + join is much better than string + string + string ...
package main
import (
"fmt"
"runtime"
)
func main() {
m := 1
s := ""