Skip to content

Instantly share code, notes, and snippets.

@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'
@snipsnipsnip
snipsnipsnip / gpm.rb
Last active August 29, 2015 14:15
gpm.rb
require 'strscan'
=begin
http://parametron.blogspot.jp/search/label/Christopher%20Strachey%E3%81%AEGPM
program = token*
token = argexpansion | macrocall | literal | constant
literal = [^$;<>,]+
argexpansion = ~ \d+
@snipsnipsnip
snipsnipsnip / machineof.cpp
Created February 2, 2015 00:37
引数で渡された Windows 実行形式ファイルの Machine タイプを判別する
//
// 引数で渡された Windows 実行形式ファイルの Machine タイプを判別する
// http://dsas.blog.klab.org/archives/51752824.html
//
#if !defined(UNICODE)
#define UNICODE
#define _UNICODE
#endif
#include <windows.h>
class Node
attr_accessor :value, :left, :right
def initialize(val)
@value = val # ノードが保持する値
@left = nil # 左側のノード
@right = nil # 右側のノード
end
end
# 描画に必要な情報を計算
@snipsnipsnip
snipsnipsnip / print-8.3-name.bat
Created January 12, 2015 19:16
print-8.3-name.bat
@echo %~s1
@snipsnipsnip
snipsnipsnip / tell-x86-or-x64.bat
Created January 7, 2015 18:29
tell-x86-or-x64.bat
@if "%PROCESSOR_ARCHITECTURE%%PROCESSOR_ARCHITEW6432%" EQU "x86" (
@echo truly x86
) else (
@echo x64
)