Skip to content

Instantly share code, notes, and snippets.

View ksss's full-sized avatar
🏠
Working from home

Yuki Kurihara ksss

🏠
Working from home
View GitHub Profile
# TempfileEnsureCloser.new.begin do |tmp|
# gz = tmp.create(["tmp", ".gz"]) # File instance
# get_object(gz)
# csv = tmp.create(["tmp", ".csv"])
# system("gunzip -c #{gz.path} > #{csv.path}")
# end #=> close and unlink all temp file
class TempfileEnsureCloser
def initialize
@tempfiles = []
end
@ksss
ksss / uuid.rb
Created June 21, 2017 08:07
uuid pack/unpack
def packuuid(uuid)
uuid.split('-').pack("H8H4H4H4H12")
end
def unpackuuid(byte)
byte.unpack("H8H4H4H4H12").join('-')
end
uuid = "43e1254f-2d13-4193-b4b4-b9520ef0e9f3"
p uuid.length #=> 36
packed = packuuid(uuid)
module TypeChecker
Error = Class.new(StandardError)
class << self
def register(this, meth, klass)
this.module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{meth}
ret = super
unless ret.kind_of?(#{klass})
raise Error, "no implicit conversion of \#{self.class} into #{klass} got \#{ret.class}"
FROM matsumotory/ngx-mruby
require 'open-uri'
class MultiDownload
def initialize(*urls)
@urls = urls
end
def to_h
@urls.map { |url|
Thread.new(url) do |url|
$ ruby pretty-argumenterror.rb
ArgumentError: wrong number of arguments (given 3, expected 2)
from: pretty-argumenterror.rb
1 | #! /usr/bin/env ruby
2 |
-> 3 | def foo(a, b)
4 | end
5 |
6 | begin
@ksss
ksss / swf_file.rb
Last active May 10, 2016 06:00
swfファイルのバイナリからRubyで縦横のサイズを取り出す ref: http://qiita.com/ksss/items/4736cb3922e10835ee96
class SwfFile
# @example:
# SwdFile.new("foo.swf")
def initialize(path)
@path = path
end
# @example:
# x_min, x_max, y_min, y_max = swf.stage_size
def stage_size
#! /usr/bin/env ruby
p [[
"0111010001101000011000010110",
"1110011010110010000001111001",
"01101111011101010010000001100",
"1100110111101110010001000000",
"11011100110010101110110011001",
"010111001000100000011001110",
"11010010111011001101001011011",
@ksss
ksss / charnames
Last active December 7, 2015 09:57
$ charnames "a" => "a: LATIN SMALL LETTER A"
#! /usr/bin/env perl
use utf8;
use Encode;
use charnames ':full';
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
if (@ARGV == 0) {