Skip to content

Instantly share code, notes, and snippets.

View znz's full-sized avatar

Kazuhiro NISHIYAMA znz

View GitHub Profile
@znz
znz / before_action.rb
Created September 4, 2019 11:37
最低限の before_action っぽいもの
class Foo
def self.before_action(hook, only:)
actions = Array(only)
self.prepend Module.new {
actions.each do |action|
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{action}
#{hook}
super
end
@znz
znz / eval_code.rb
Last active September 19, 2018 12:30
jekyll plugin to eval trusted code
require 'jekyll'
require 'open3'
module Jekyll
class EvalCode < Jekyll::Generator
def generate(site)
@site = site
site.pages.each { |page| eval_code(page) if page.html? }
site.posts.docs.each { |page| eval_code(page) }
end
@znz
znz / gist:3c54b7591dace43a55479e9ebbce12f8
Created August 16, 2018 14:40
BDST 周りの isdst
% zdump -v Europe/London | egrep '194[0-5]'
Europe/London Sun Feb 25 01:59:59 1940 UT = Sun Feb 25 01:59:59 1940 GMT isdst=0 gmtoff=0
Europe/London Sun Feb 25 02:00:00 1940 UT = Sun Feb 25 03:00:00 1940 BST isdst=1 gmtoff=3600
Europe/London Sun May 4 00:59:59 1941 UT = Sun May 4 01:59:59 1941 BST isdst=1 gmtoff=3600
Europe/London Sun May 4 01:00:00 1941 UT = Sun May 4 03:00:00 1941 BDST isdst=1 gmtoff=7200
Europe/London Sun Aug 10 00:59:59 1941 UT = Sun Aug 10 02:59:59 1941 BDST isdst=1 gmtoff=7200
Europe/London Sun Aug 10 01:00:00 1941 UT = Sun Aug 10 02:00:00 1941 BST isdst=1 gmtoff=3600
Europe/London Sun Apr 5 00:59:59 1942 UT = Sun Apr 5 01:59:59 1942 BST isdst=1 gmtoff=3600
Europe/London Sun Apr 5 01:00:00 1942 UT = Sun Apr 5 03:00:00 1942 BDST isdst=1 gmtoff=7200
Europe/London Sun Aug 9 00:59:59 1942 UT = Sun Aug 9 02:59:59 1942 BDST isdst=1 gmtoff=7200
@znz
znz / RubyPrize.md
Last active January 29, 2020 01:32
Ruby Prize 候補者(敬称略)
@znz
znz / nkf-proxy.rb
Last active April 6, 2018 12:11
JIS (IRCnet) <-> UTF-8 (IRC client) proxy
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'nkf'
require 'socket'
Socket.tcp_server_loop(6668) do |socket, client_addr|
begin
begin
ircnet_socket = Socket.tcp('irc.ircnet.ne.jp', 6667)
@znz
znz / test.sh
Created August 13, 2017 06:05
git merge と git rebase の挙動の違いの確認
#!/bin/sh
set -eux
mkdir /tmp/x
cd /tmp/x
git init
echo hello > hello.txt
git add .
git commit -m "Initial commit"
git remote add origin /tmp/repo
mkdir /tmp/repo
@znz
znz / r.rb
Created January 23, 2017 15:24
ruby の正規表現の ^$ と \A\z の速度比較
require 'benchmark'
Benchmark.bmbm do |x|
n = 1000000
x.report('^$ 0 ') { n.times { 0.class.to_s =~ /^(Integer|Fixnum|FalseClass|TrueClass|NilClass)$/ } }
x.report('\A\z 0 ') { n.times { 0.class.to_s =~ /\A(Integer|Fixnum|FalseClass|TrueClass|NilClass)\z/ } }
x.report('^$ nil') { n.times { nil.class.to_s =~ /^(Integer|Fixnum|FalseClass|TrueClass|NilClass)$/ } }
x.report('\A\z nil') { n.times { nil.class.to_s =~ /\A(Integer|Fixnum|FalseClass|TrueClass|NilClass)\z/ } }
x.report('^$ x ') { n.times { x.class.to_s =~ /^(Integer|Fixnum|FalseClass|TrueClass|NilClass)$/ } }
x.report('\A\z x ') { n.times { x.class.to_s =~ /\A(Integer|Fixnum|FalseClass|TrueClass|NilClass)\z/ } }
end
@znz
znz / katakana.txt
Created August 23, 2016 14:00
UnicodeのKatakanaプロパティでマッチした文字一覧
% rbenv exec irb -r irb/completion --simple-prompt
>> RUBY_DESCRIPTION
=> "ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin14]"
>> (0x0...0xD800).lazy.map{|u|u.chr(Encoding::UTF_8)}.grep(/\p{Katakana}/).each{|c|print c}
ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶヷヸヹヺヽヾヿㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ㋐㋑㋒㋓㋔㋕㋖㋗㋘㋙㋚㋛㋜㋝㋞㋟㋠㋡㋢㋣㋤㋥㋦㋧㋨㋩㋪㋫㋬㋭㋮㋯㋰㋱㋲㋳㋴㋵㋶㋷㋸㋹㋺㋻㋼㋽㋾㌀㌁㌂㌃㌄㌅㌆㌇㌈㌉㌊㌋㌌㌍㌎㌏㌐㌑㌒㌓㌔㌕㌖㌗㌘㌙㌚㌛㌜㌝㌞㌟㌠㌡㌢㌣㌤㌥㌦㌧㌨㌩㌪㌫㌬㌭㌮㌯㌰㌱㌲㌳㌴㌵㌶㌷㌸㌹㌺㌻㌼㌽㌾㌿㍀㍁㍂㍃㍄㍅㍆㍇㍈㍉㍊㍋㍌㍍㍎㍏㍐㍑㍒㍓㍔㍕㍖㍗=> nil
>> (0xE000...0xFFFF).lazy.map{|u|u.chr(Encoding::UTF_8)}.grep(/\p{Katakana}/).each{|c|print c}
ヲァィゥェォャュョッアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン=> nil
>> (0x10000...0x10FFFF).lazy.map{|u|u.chr(Encoding::UTF_8)}.grep(/\p{Katakana}/).each{|c|print c}
𛀀=> nil
>>
@znz
znz / bootstrap3-input-file.html
Last active April 1, 2019 09:56
bootstrap3 input file test
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>bootstrap3のinput[file]のテスト</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
</head>
<body>
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
$str = "abcd"
data = %w[
0 14 5 14
32 0 38 12
46 28 0 7
33 18 44 0
]
data = data.map(&:to_i)