Skip to content

Instantly share code, notes, and snippets.

View shotat's full-sized avatar
:octocat:
Enjoy!

Shota Terashita shotat

:octocat:
Enjoy!
View GitHub Profile
@shotat
shotat / usecase.go
Created March 3, 2018 04:40
Implementation of usecae
package main
// NOTE: change to event driven
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
class Array
# avoid to conflict `#to_enum`
def to_enumerator
enum = Object.const_set('Enum', Module.new)
enum.tap do
each_with_index do |e, idx|
enum.define_singleton_method(e) { idx }
end
end
end
@shotat
shotat / sushi.sh
Created April 7, 2017 06:24
Gitの履歴からinsertion, deletionを抜き出すやつ
#!/bin/bash
IFS=$'\n'
since_date='2010-01-01'
git_authors() {
git log --shortstat --since="${since_date}" | grep -e '^Author' | awk -F' ' '{$1="";$NF=""; print}' | sort | uniq | sed -e 's/^ //g'
}
git_stat() {
@shotat
shotat / csrf.rb
Created March 15, 2017 06:15
Railsのcsrf対策処理のencodingと検証処理をざっくり
require 'securerandom'
require 'base64'
# https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/csrf_helper.rb#L20-L27
# token = 'jYZcRG05o2IRERhLVqwNVZREdUe4XB0okJRcBUrBQlQ='
# <meta name="csrf-param" content="authenticity_token" />
# <meta name="csrf-token" content="sZu120+MWcK2pEomGML/5SsRSgmGaYOwt0ABKTu663MV0FPC2ssoQuhRl2FXRAn+jEreDewPRIq0ZRh4AoTfxA==" />
class Csrf
AUTHENTICITY_TOKEN_LENGTH = 32
@shotat
shotat / concurrent.rb
Last active November 16, 2016 02:45
concurrent-ruby
require 'benchmark'
require 'concurrent'
require 'net/http'
require 'uri'
require 'webmock'
class AsyncHttp
include Concurrent::Async # important
def initialize(base_uri)
@shotat
shotat / jQuery.js
Created October 7, 2016 03:05
debug jQuery on browser
if (!window.jQuery) {
document.body.appendChild((function(){
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js";
return s;
})());
}
@shotat
shotat / retry.rb
Created September 18, 2016 10:24
retry.rb
retries = 0
begin
service.update(record)
rescue VendorDeadlockError => e
raise if retries >= 3
retries += 1
logger.warn("API failure: #{e}, retrying...")
sleep(5**retries)
retry
@shotat
shotat / Lock.rb
Last active September 18, 2016 09:54
Lock.rb
class Lock
def self.acquire
lock = new # リソース初期化
lock.exclusive_lock!
if block_given?
yield(lock)
else
lock
end
@shotat
shotat / Version.rb
Created September 18, 2016 05:37
Version.rb
class Version
include(Compareble)
attr_accessor(:major, :minor, :patch)
def initialize (version)
@major, @minor, @patch =
version.split('.').map(&:to_i)
end
def <=> (other)
@shotat
shotat / install_octave.md
Last active May 5, 2016 00:53
octave installどハマりした
$ brew tap homebrew/science
$ brew install octave

$ brew uninstall gnuplot
$ brew install Caskroom/cask/xquartz
$ brew cask install aquaterm
$ brew install gnuplot --with-aquaterm --with-x11

$ gnuplot