Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
snipsnipsnip / prepare-cacert-pem.rb
Last active March 1, 2016 13:39
prepare-cacert-pem.rb
require 'fileutils'
require 'openssl'
require 'open-uri'
CACERT_URI = 'https://curl.haxx.se/ca/cacert.pem'
def main
if force = ARGV.include?('-f') || ARGV.include?('--force')
u = FileUtils::Verbose
else
@snipsnipsnip
snipsnipsnip / parse_yaml_with_pretty_error.rb
Created February 26, 2016 15:05
def parse_yaml_with_pretty_error(str)
def parse_yaml_with_pretty_error(str)
YAML.load(str)
rescue Psych::SyntaxError => e
range = e.line - 5..e.line + 5
str.lines[range].zip(range) {|s, n| warn "#{n + 1}: #{s}#{%_#{' ' * (e.column - 1)}^_ if n + 1 == e.line}" }
raise
end
@snipsnipsnip
snipsnipsnip / eval-ruby.js
Last active December 26, 2015 11:29
mery macro: eval-ruby.js
if (document.selection.IsEmpty) {
document.selection.EndOfLine(false, mePosLogical)
document.selection.StartOfLine(true, mePosLogical)
}
var shell = new ActiveXObject('WScript.Shell')
shell.CurrentDirectory = document.Path || shell.SpecialFolders("Desktop")
var exec = shell.Exec('rubyw.exe -rtimeout -e "STDERR.reopen STDOUT; $0 = STDIN.gets.rstrip; print begin; Timeout.timeout(2) { eval($_ = STDIN.read) }; rescue Exception; %_#<#{$!.class}:#{$!.message}>_ end"')
exec.StdIn.WriteLine(document.FullName)
@snipsnipsnip
snipsnipsnip / indented_forest.rb
Last active December 13, 2015 08:36
継承つきネスト箇条書き
require 'yaml'
# StringForest = nil | (String, StringForest)[]
# String -> StringForest
def parse_indented_forest(str)
str = str.gsub(/^( *)([^": \n][^":\n]*)$/, '\1"\2":')
handle_includes YAML.load(str)
end
def handle_includes(tree)
@snipsnipsnip
snipsnipsnip / tom.rb
Created November 26, 2015 11:36
tom.rb: primitive JSP lint
# coding: utf-8
#
# tom: JSPのタグ開閉の対応チェッカ
# MIT license, @snipsnipsnip
#
=begin
JSPの文法は普通のHTMLと違ってかなりゆるい。
@snipsnipsnip
snipsnipsnip / backtick.bat
Last active February 9, 2019 07:28
backtick.bat: poor man's xargs
@ruby^
-Ebinary:binary^
-e "args = STDIN.readlines.map(&:strip)"^
-e "abort 'empty args' if args.empty?"^
-e "cmd = ARGV.empty? ? ['type'] : ARGV"^
-e "ai = cmd.any? {|c| c.include?('{}') }"^
-e "cmds = ai ? args.map {|a| cmd.map {|z| z.gsub(/\{\}/, a).gsub(/@@/, '{}') } } : [cmd + args]"^
-e "cmds.each {|c| warn 'backtick: ' + c.join(' '); system(*c) || exit($?.to_i) }"^
-- %*
@snipsnipsnip
snipsnipsnip / waitpid.bat
Created September 7, 2015 03:49
waitpid.bat
@ruby -x "%~f0" %* & exit /b
#!ruby
pid = Integer(ARGV[0]).to_s
while `tasklist.exe /FI "PID eq #{pid}"`.include?(pid)
sleep 1
end
@snipsnipsnip
snipsnipsnip / io_hasher.rb
Created July 21, 2015 17:24
io_hasher.rb: digests an IO
require 'digest'
class IOHasher
def initialize(digest=Digest::MD5.new, buf='\0' * 4096)
@digest = digest
@buf = buf
@chunk_size = buf.size
end
def hash_of(io)
@snipsnipsnip
snipsnipsnip / stdlib-digest-openssldigest-benchmark.rb
Created July 20, 2015 07:17
stdlib-digest-openssldigest-benchmark.rb
require 'benchmark'
require 'digest'
require 'openssl'
lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.' * 1000
n = 10000
Benchmark.bmbm do |b|
b.report('digest') { n.times { Digest::SHA1.digest(lorem) } }
b.report('openssl') { n.times { OpenSSL::Digest::SHA1.new.digest(lorem) } }
@snipsnipsnip
snipsnipsnip / update-onamae-ddns.rb
Last active September 18, 2021 03:58
update-onamae-ddns.rb: お名前.comのダイナミックDNSの登録を更新
require 'socket'
require 'openssl'
abort "usage: #$0 userid password {'*'|''|subdomain} example.com" if ARGV.size != 4
UserID, Password, Subdomain, Domain = ARGV
OnamaePubkey = '224ad8a1d3af62ad5638091d485d7d9bf400e5d106ed0d5bdb1d96bb0ee6d2a5'
DDNSServer = 'ddnsclient.onamae.com'