Skip to content

Instantly share code, notes, and snippets.

# This code is based on the code at blow:
# https://github.com/fastruby/fast-ruby/tree/main/code/enumerable
require "benchmark/ips"
ARRAY = [*1..100]
def fast
ARRAY.each.with_index(1) do |number, index|
index
end
@libkazz
libkazz / gist:2879396
Created June 6, 2012 02:03
count urls in sitemap.xml.gz
zcat public/sitemap1.xml.gz | ruby -anle 'puts $_.gsub("<url>", "#").gsub(/[^#]*/, "").length'
@libkazz
libkazz / Gemfile
Last active October 6, 2022 02:25
source "https://rubygems.org"
gem "activerecord", '~> 7.0'
gem "sqlite3"
@libkazz
libkazz / sakuma.js
Last active May 14, 2020 14:24
22種類のものが隣同士にならないように、縦におなじものがこないように縦6×横100にランダムに並べる
const randomArray = (w, h, v) => {
const a = Array.from(Array(v), (_,k) => k)
let t = []
for(let y=0; y<h; y++){
t.push([]);
for(let x=0; x<w; x++){
let g = t.map(r => r[x]).concat([t[y][x-1]]);
let f = a.filter(e => !g.includes(e));
t[y][x] = f[Math.floor(Math.random()*f.length)];
}
@libkazz
libkazz / bundle-update.sh
Last active October 18, 2019 07:29
bundle update をする shell
bundle update
for m in `git d Gemfile.lock | egrep '^\+ \w' | sed -e 's/^\+ //' -e 's/ (.*//' | sort -u`; do
git checkout Gemfile.lock
bundle update $m
git add -u Gemfile.lock
git commit -m ":up: $m"
git push origin HEAD
echo "============== $m ==============="
done
# コンテナにログインする
docker ps
docker exec -it {CONTAINER ID} bash
# コンテナ削除
docker ps -a -q | xargs docker rm
# イメージ削除(コンテナ削除後)
docker images -q | xargs docker rmi
@libkazz
libkazz / .env
Created August 13, 2019 03:51
wordpress docker-compose
WORDPRESS_DB_NAME=wordpress
WORDPRESS_DB_USER={input user name}
WORDPRESS_DB_PASSWORD={input password}
MYSQL_RANDOM_ROOT_PASSWORD=yes
MYSQL_DATABASE=wordpress
MYSQL_USER={input user name}
MYSQL_PASSWORD={input password}
@libkazz
libkazz / gist:9650817a454126f4e9bb0a725dac0b16
Last active August 3, 2019 09:59
RubyでExcel列文字列を列番号に変換する方法
def row_number(str)
str.bytes.reverse.each.with_index(0).reduce(0) do |n, (c, i)|
n += (26 ** i * (c.ord - 64))
end
end
('A'..'BZ').each do |str|
p [str, row_number(str)]
end
@libkazz
libkazz / simulation-sample.rb
Created August 19, 2018 05:34
proxy sample
require 'rack-proxy'
module SimulationSample
class Proxy < Rack::Proxy
def rewrite_env(env)
env
end
end
def self.call(env)
@libkazz
libkazz / orphaned_partial_check.rb
Created July 17, 2018 02:07
使われていない partial の簡易チェックスクリプト
def target_partial
Dir['app/views/front/{shared,sections,sidebar}/**/*'].each_with_object([]) do |f, list|
next unless File.file?(f)
list << [
File.dirname(f).sub('app/views/', ''),
File.basename(f).sub(/^_/, '').sub('.slim', '').sub('.html.erb', '')
].join('/')
end
end