Skip to content

Instantly share code, notes, and snippets.

View chsh's full-sized avatar
👍
Love your code.

CHIKURA Shinsaku chsh

👍
Love your code.
View GitHub Profile
@chsh
chsh / url_extractor.rb
Created November 6, 2010 03:42
Restore original url from the url made from some url shortening services.
require 'net/http'
require 'uri'
# Restore original url from the url made from some url shortening services.
#
# Currently tested:
# bit.ly(j.mp), ustre.am, tinyurl
class URLExtractor
def self.expand_url(short_url)
@chsh
chsh / bubble_values.rb
Created December 8, 2010 14:28
BubbleValues keeps volatile values cleared in few seconds.
class BubbleValue
  def initialize(hash, key, timeout = 3)
    @hash = hash
    @key = key
    Thread.new {
      sleep timeout
      @hash[@key] = nil
    }
  end
end
@chsh
chsh / sis2lite.rb
Created December 20, 2010 04:21
gitosis.conf to conf/gitolite.conf convert script
# sis2lite.rb
class RepoRep
def initialize()
@repomap = {}
end
def save_state(group, members, writable, readonly)
a_members = members.strip.split(/\s+/)
@chsh
chsh / langs_builder.rb
Created February 11, 2011 04:41
Build lang selection master data using Google language_tools page. :-D
# encoding: utf-8
require 'open-uri'
require 'yaml'
require 'rubygems'
require 'nokogiri'
class LangsBuilder
def dump_langs(writer_or_file)
if writer_or_file.is_a? String
@chsh
chsh / dozens.rb
Created February 17, 2011 08:22
Ruby class to access Dozens API.
require 'net/http'
require 'uri'
require 'json'
class Dozens
API_BASE = 'http://dozens.jp/api/'
ZONE_BASE = API_BASE + 'zone'
RECORD_BASE = API_BASE + 'record'
@chsh
chsh / rvm-install-1.9.2.txt
Created February 23, 2011 08:14
Howto install ruby 1.9.2-p180 using rvm with readline package.
Mac OS X 10.6.6 において
rvm install 1.9.2-p180
で readline.c がエラーになったので、
rvm package install readline
をやって
@chsh
chsh / integer_ext.rb
Created February 26, 2011 15:19
Number formatting with columns. You should have better implementation. :-P
class Integer
def to_cs(numcol = 3)
unit = 10 ** numcol
reps = '\d' * numcol
neg = false
str = self.to_s
if self <= -unit
neg = true
str = (-self).to_s
@chsh
chsh / build-path-with-self.js
Created May 7, 2011 15:57
Build javascript load path using a dir itself.
var SCRIPT_NAME = 'my_name.js';
function getScriptPath(target) {
var list = document.getElementsByTagName("script");
var i = list.length;
while (i--) {
var src = list[i].src;
if (src) {
var result = src.match(/^(.*)([^\/]+\.js)$/);
if (result) {
if (result[2] == SCRIPT_NAME) {
@chsh
chsh / cx.pow.firewall.plist
Created May 10, 2011 15:22
Route incoming port:80 connection to other using ipfw.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>cx.pow.firewall</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
@chsh
chsh / pathable_hash.rb
Created May 29, 2011 16:29
Add Hash#path method to lookup deep structured hash value.
# pathable_hash.rb
# Add Hash#path method to lookup deep stuructured hash value.
# e.g)
# h0 = {a: {b: {c: 1}}}
# h0.path '/a/b/c' => 1
# h0.path 'a/b/c' => 1 # heading slash has no meaning.
# h1 = {a: {b: [1,2,{c: 3}]}
# h1.path '/a/b[2]/c' => 3 # [\d] means picking up array item.
# path syntax is inspired by xpath, but not same. :-)