Skip to content

Instantly share code, notes, and snippets.

@to4iki
to4iki / proc.rb
Created August 29, 2015 08:26
proc
def calc(b)
b.call(3, 2)
end
#def calc(&block)
# block.call
#end
# calc { |a, b| p a+b }
@to4iki
to4iki / collision_detection.rb
Last active August 29, 2015 13:58
衝突判定
CODE_LENGTH = 8
def generate_code
code = SecureRandom.hex(6).hex.to_s(36)
code = code[0, CODE_LENGTH]
code += ('A'..'Z').to_a.sample(CODE_LENGTH - code.length).join('')
code.upcase
end
def collision_detection(re_count)
@to4iki
to4iki / 2014040300000_add_code_to_users.rb
Created April 4, 2014 01:59
存在するテーブルにカラム追加 + 値代入
class AddCodeToUsers < ActiveRecord::Migration
def change
add_column :users, :code, :string, limit: 8, after: :support_code, comment: 'コード'
User.reset_column_information
reversible do |dir|
dir.up do
# 衝突するかしないかは運任せな所あり
User.find_each do |u|
u.update(invited_code: SecureRandom.hex(4).upcase)
@to4iki
to4iki / rmagic_image_info.rb
Last active August 29, 2015 13:59
カレントディレクトリ配下のpngファイルの画像サイズを分類する
require 'RMagick'
require 'find'
require "FileUtils"
# アニメーション GIF が考慮されて画像は配列で読み込まれるので、
# 配列の先頭画像を取得する。
SMALL_SIZE = 150
MEDIUM_SIZE = 200
BIG_SIZE = 300
@to4iki
to4iki / symlink.sh
Created May 16, 2014 14:26
create symbolic link scripts
@to4iki
to4iki / tmp.md
Created August 1, 2014 16:57
README.md tmpl
@to4iki
to4iki / YearUtil.scala
Created August 4, 2014 16:27
閏年判定
import java.util.Calendar
object YearUtil extends App {
println(countLeapYear(1989, 2014))
/**
* 閏年判定
*
* - 西暦年が4で割り切れる年は閏年
@to4iki
to4iki / PrimeNumber.scala
Created September 3, 2014 16:48
素数アルゴリズム
/**
* 素数アルゴリズム
*
* エラトステネスの篩
* - 整数のリストに2からxまでの数を入れる
* - リストの先頭に行き、その数の倍数をふるい落とす
* - リストの先頭の値がxの平方根になるまで繰り返す
*/
object PrimeNumber {
def main(args: Array[String]) {
@to4iki
to4iki / post-checkout
Created November 12, 2014 06:42
以下の設定を.git/hooks/以下のpost-checkoutファイルに書いてchmod +x post-checkoutしておけばブランチ切り替えごとにPodfileの更新があれば自動的にpod installするようになる。
#!/bin/sh
# parse this format
# x3de1b0 HEAD@{0}: checkout: moving from xxxx to yyyyy
BEFORE_BRANCH=`git reflog | head -1 | cut -f 6 -d ' '`
AFTER_BRANCH=`git reflog | head -1 | cut -f 8 -d ' '`
DIFF=`git diff ${BEFORE_BRANCH} ${AFTER_BRANCH} --name-only | grep Podfile`
if [ -n "${DIFF}" ]; then
bundle exec pod install # pod install
(function(global) {
"use strict;"
// Class ------------------------------------------------
function YourModule() {
};
// Header -----------------------------------------------
YourModule["prototype"]["method"] = YourModule_method; // YourModule#method(someArg:any):void