Skip to content

Instantly share code, notes, and snippets.

Avatar

Kazuhiro NISHIYAMA znz

View GitHub Profile
@znz
znz / before_action.rb
Created Sep 4, 2019
最低限の before_action っぽいもの
View before_action.rb
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 Sep 19, 2018
jekyll plugin to eval trusted code
View eval_code.rb
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
View gist:3c54b7591dace43a55479e9ebbce12f8
% 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 Jan 29, 2020
Ruby Prize 候補者(敬称略)
View RubyPrize.md
@znz
znz / nkf-proxy.rb
Last active Apr 6, 2018
JIS (IRCnet) <-> UTF-8 (IRC client) proxy
View nkf-proxy.rb
#!/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 Aug 13, 2017
git merge と git rebase の挙動の違いの確認
View test.sh
#!/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 Jan 23, 2017
ruby の正規表現の ^$ と \A\z の速度比較
View r.rb
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 Aug 23, 2016
UnicodeのKatakanaプロパティでマッチした文字一覧
View katakana.txt
% 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 Apr 1, 2019
bootstrap3 input file test
View bootstrap3-input-file.html
<!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>
View abcd.rb
#!/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)