Skip to content

Instantly share code, notes, and snippets.

@koki-h
koki-h / collatz.rb
Last active September 4, 2021 07:59
任意の自然数maxまでの自然数についてコラッツ予想が正しいかを確かめるプログラム
# 任意の自然数maxまでの自然数についてコラッツ予想が正しいかを確かめるプログラム
# 収束しない値(予想に反する値)が現れた場合はスタックオーバーフローでエラーになる。(とはいえスタックが足りないだけでいつか収束するのかもしれない。)
# コラッツ予想とは
# 「どんな正の整数も、偶数なら2で割り、奇数なら3倍して1を足す。この操作を繰り返せば、必ず最後は1になるだろう」
# see: https://digital.asahi.com/articles/ASP937HM6P8ZULBJ00T.html
require 'tempfile'
def collatz(n)
$tempio.print n
if (n % 2) == 0
@koki-h
koki-h / get_download_link.js
Last active July 30, 2021 04:17
違法アップロードサイトからダウンロードリンクを抽出する
@koki-h
koki-h / vaccine.rb
Last active June 4, 2021 15:16
ワクワクチンチンが出るまでランダムに"ワ","ク","チ","ン"を出す
# inspired by https://anond.hatelabo.jp/20210604154733
CHAR_BOX=%w|ワ ク チ ン|
TARGET_STR="ワクワクチンチン"
current_str_a = []
current_str = ""
while true do
current_str_a.shift if current_str_a.size >= TARGET_STR.size
c = CHAR_BOX.sample
current_str_a << c
@koki-h
koki-h / Vagrantfile
Last active June 18, 2020 04:18
Plastic Rails( https://github.com/koki-h/plastic_rails )のデバッグ用Virtualboxを作成するVagrantfile。ruby, bundler, docker (compose)が最初から入る。
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-18.04"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provision "shell", inline: <<-SHELL
apt-get install ruby -y
gem install bundler
curl -fsSL https://get.docker.com -o get-docker.sh
@koki-h
koki-h / Vagrantfile
Created May 28, 2020 10:30
docker-compose が最初から動くVagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-18.04"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provision "shell", inline: <<-SHELL
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
usermod -aG docker vagrant
@koki-h
koki-h / semaphore.js
Created May 18, 2020 04:06 — forked from gregkorossy/semaphore.js
A simple implementation of a semaphore in JS
function Semaphore(max) {
var counter = 0;
var waiting = [];
var take = function() {
if (waiting.length > 0 && counter < max){
counter++;
let promise = waiting.shift();
promise.resolve();
}
@koki-h
koki-h / semaphore.js
Last active May 18, 2020 04:06 — forked from gregkorossy/semaphore.js
A simple implementation of a semaphore in JS
function Semaphore(max) {
// 排他制御のためのセマフォ
// https://gist.github.com/Gericop/e33be1f201cf242197d9c4d0a1fa7335
var counter = 0;
var waiting = [];
var take = function() {
if (waiting.length > 0 && counter < max){
counter++;
@koki-h
koki-h / locale_from_migration.rb
Created February 11, 2020 08:03
コメントを利用してマイグレーションファイルからロケール(yaml)を作成
require 'psych' # to_yamlを使うため
def table_name(line)
line.scan(/create_table :(.+), comment: \"(.+)"/)
$LOCALE["ja"]["activerecord"]["models"][$1] = $2
$LOCALE["ja"]["activerecord"]["attributes"][$1] = {}
$1
end
def column_name(line)
@koki-h
koki-h / conv.rb
Created February 6, 2020 02:24
カレントディレクトリ内のcsvファイルをUTF-8からShift_JISへ変換
Dir.glob("./*.csv") do |f|
p f
`mv #{f} #{f}.bak`
`nkf -W -s #{f}.bak > #{f}`
end
@koki-h
koki-h / set_table_name_with_filename.rb
Created January 17, 2020 07:37
ActiveRecordモデルクラスの参照先テーブル名をモデルクラスの定義されているファイル名と同じに設定するスニペット
require 'tempfile'
require 'fileutils'
tables = %w|
table1
table2
table3
table4
table5
table6