Skip to content

Instantly share code, notes, and snippets.

@sunaot
sunaot / rewrite rules
Created July 26, 2011 00:45
rewrite rules for www.ruby-lang.org. see [ruby-list:48242]
RewriteRule ^ja/man/html/_A4CFA4B8A4E1A4CB.html(.*)$ http://doc.ruby-lang.org/ja/1.9.2/doc/spec=2fintro.html$1 [L,R=301]
RewriteRule ^ja/man/html/_A5B3A5DEA5F3A5C9.html(.*)$ http://doc.ruby-lang.org/ja/1.9.2/doc/spec=2fcommands.html$1 [L,R=301]
RewriteRule ^ja/man/html/Ruby_A4CEB5AFC6B0.html(.*)$ http://doc.ruby-lang.org/ja/1.9.2/doc/spec=2frubycmd.html$1 [L,R=301]
RewriteRule ^ja/man/html/-(.*)$ http://doc.ruby-lang.org/ja/-$1 [L,R=301]
RewriteRule ^ja/man/html/-(.*)$ http://doc.ruby-lang.org/ja/-$1 [L,R=301]
RewriteRule ^ja/man/html/_B4C4B6ADCAD1BFF4.html(.*)$ http://doc.ruby-lang.org/ja/1.9.2/doc/spec=2fenvvars.html$1 [L,R=301]
RewriteRule ^ja/man/html/-(.*)$ http://doc.ruby-lang.org/ja/-$1 [L,R=301]
RewriteRule ^ja/man/html/_A5AAA5D6A5B8A5A7A5AFA5C8.html(.*)$ http://doc.ruby-lang.org/ja/1.9.2/doc/spec=2fobject.html$1 [L,R=301]
RewriteRule ^ja/man/html/_A5AFA5E9A5B9.html(.*)$ http://doc.ruby-lang.org/ja/1.9.2/doc/spec=2fclass.html$1 [L,R=301]
RewriteRule ^ja/man/html/_BCC2B9D4.html(.*)$ http://doc.ruby-lan
<?php
// public ReflectionMethod::getClosure ( object $object ) alternative
function toClosure($class_name, $method_name, $obj = null) {
if (!is_null($obj) and !is_a($obj, $class_name)) throw new \InvalidArgumentException('invalid object is given.');
$func = new ReflectionMethod($class_name, $method_name);
if ($func->isStatic()) {
return function() use($func) {
$func->invokeArgs(null, func_get_args());
};
} else {
<?php
// see https://gist.github.com/1106254 about toClosure() function.
class SpecificException extends Exception {};
class Sample
{
// Exectute Around Method and try-catch-finally sample in PHP.
function inLock($callback) {
$unlock = \toClosure(get_class(), 'unlock', $this);
$ensure = function() use($unlock) { $unlock(); };
$this->lock();
@sunaot
sunaot / sugoroku.js
Created July 29, 2011 06:40
JavaScript 教室の練習問題。スゴロクをつくってみようのコーナー
// スゴロク
// Start Goal
// 0 -- 1 -- 2 -- 3 -- 4
// dice!
//
// * 0 から 4 までのマスがあります
// * 0 から 2 のマスにいるとき、1 ターンにつき 1 進みます
// * 3 のマスにいるときは、サイコロを振り、1 か 6 のときのみ 1 進みます。 2 から 5 が出たときは 3 のマスへ残ったまま次のターンへ進みます
// * 4 まで来たらゴールなので、ゲームを終えます
// * ターンはループで回る一回を 1 ターンにします
@sunaot
sunaot / static_variable.php
Created August 15, 2011 10:14
how static variable works in PHP.
<?php
class Base
{
static public $name = 'BASE';
function stamp($name) {
static::$name = $name;
}
}
class A extends Base
@sunaot
sunaot / how_to_access_static_private_variable_destructively.php
Created August 15, 2011 11:04
Accessing static private variable hack using closure.
<?php
class Sample
{
static private $value = 0;
static function privateHack() {
$hack =& static::$value;
return function($val) use (&$hack) {
$hack = $val;
};
}
@sunaot
sunaot / dsl_sample.php
Created August 15, 2011 11:51
DSL in PHP sample. See Entity::customize method.
<?php
class HackObject
{
public $_entityName;
public $_attributeNames;
function entityName($val) { $func = $this->_entityName; $func($val); }
function attributeNames($val) { $func = $this->_attributeNames; $func($val); }
}
class EntityBase
@sunaot
sunaot / groupBy.sql
Created October 6, 2011 15:54
Haskell の groupBy の動作を誤解してるときに SQL で書いてみたそれっぽい動きをするもの
-- id num id+1 num
-------------------------
-- 1 1 2 2
-- 2 2 3 3
-- 3 3 4 4
-- 4 4 5 5
-- 5 5 6 6
-- 6 6 7 7
-- 7 7 8 8
-- 8 8 9 9
@sunaot
sunaot / designing_testable_module_in_MVC.md
Created October 11, 2011 10:33
MVC な WAF でテストを書くときの設計方針

MVC な WAF でテストを書くための設計について

Controller から DB アクセスをしない

  • Controller が DB アクセスをするのを許すと、フローのテストが遅くなる
    • DB アクセスが必要な処理は Model へ追いやる
    • QUERY_STRING, ENV あたりを入力とする Controller のフローのテストと、DB や memcached を入力 とする Model のテストは分離するとテストしやすい
    • Controller と Model, View などの API の切れ目で抽象化してテスト可能にしておくとやりやすいので、 切れ目をフレームワークで強制したり、運用ルールで縛ると最低限のテスト可能性が確保された設計に
@sunaot
sunaot / coding_guideline.md
Created November 9, 2011 02:11
コーディングガイドライン

コーディング指針

基本方針

こんなコードはよいコード

  • 読みやすいことはよいことだ
    • 見た目が単純だと読みやすい
    • 皆がよく知っている構成は読みやすい (標準に従う。すでに知っているもの (Unix のコマンドとか) に従う。すでにあるものに従う)
  • 書かないことはよいことだ