Skip to content

Instantly share code, notes, and snippets.

View 3014zhangshuo's full-sized avatar
🎯
Coding

张硕 3014zhangshuo

🎯
Coding
View GitHub Profile
@3014zhangshuo
3014zhangshuo / curl.md
Created April 15, 2019 09:21 — forked from subfuzion/curl.md
curl POST examples

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.

@3014zhangshuo
3014zhangshuo / array_new.rb
Created March 27, 2019 07:19
Ruby Array.new
collection = Array.new(3, 'abc')
collection[0] << 'd'
# all collection item changed, because item value is references
collection # => ['abcd', 'abcd', 'abcd']
collection = Array.new(3) { 'abc' }
collection[0] << 'd'
# pass a block, block executed 3 times, each time "abc" is new string not references
collection # => ['abcd', 'abc', 'abc']
@3014zhangshuo
3014zhangshuo / memoizer.rb
Created March 9, 2019 13:00
ruby memoized
module RubyMemoized
class Memoizer
def initialize(content, method)
@content = content
@method = method
end
def call(*args, &block)
return cache[[args, block]] if cache.has_key?(args, block])
cache[[args, block]] = content.send(method, *args, &block)
@3014zhangshuo
3014zhangshuo / migration.rb
Created January 15, 2019 08:14
Rails error unkown primary key for table but the id exist
class AddPrimaryKeyToCollections < ActiveRecord::Migration
def change
execute "ALTER TABLE collections ADD PRIMARY KEY (id);"
end
end
# https://stackoverflow.com/questions/18056162/getting-unknown-primary-key-for-table-while-the-id-is-there
@3014zhangshuo
3014zhangshuo / file_io.rb
Created January 15, 2019 08:08
FileIO: Writing strings as Carrierwave uploads
class FileIO < StringIO
def initialize(stream, filename)
super(stream)
@original_filename = filename
end
attr_reader :original_filename
end
# https://makandracards.com/makandra/50526-fileio-writing-strings-as-carrierwave-uploads
@3014zhangshuo
3014zhangshuo / anonymous_function.js
Created December 28, 2018 08:32
anonymous function example in ruby and javascript
var sum = function (a, b) {
return a + b;
}
@3014zhangshuo
3014zhangshuo / js_var.rb
Created December 28, 2018 08:12
ruby code simulate javascript var keyword assign value
def var(_);end
var name = "z"
@3014zhangshuo
3014zhangshuo / async-await.js
Created December 21, 2018 07:56 — forked from wesbos/async-await.js
Simple Async/Await Example
// 🔥 Node 7.6 has async/await! Here is a quick run down on how async/await works
const axios = require('axios'); // promised based requests - like fetch()
function getCoffee() {
return new Promise(resolve => {
setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee
});
}
@3014zhangshuo
3014zhangshuo / branch-count.sh
Created November 7, 2018 04:16 — forked from rwaldron/branch-count.sh
Count number of branches in a repo
git branch | wc -l
@3014zhangshuo
3014zhangshuo / awk-example.sh
Created September 8, 2018 04:30 — forked from raecoo/awk-example.sh
awk/grep commands for Rails log analysis
# Access number
cat production.log | grep "^Processing" | wc | awk '{print $1}'
# Each IP access number
cat production.log | grep “^Processing” | awk ‘{print $4}’ | uniq -c
# Independent IP number
cat production.log | grep "^Processing" | awk '{print $4}' | uniq | wc | awk '{print $1}'
cat production.log | grep “^Processing” | awk ‘{print $4}’ | uniq | wc -l