This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# sort_byの実装イメージ(公式リファレンスより) | |
class Array | |
def sort_by | |
self.map {|i| [yield(i), i] }. | |
sort {|a, b| a[0] <=> b[0] }. | |
map {|i| i[1]} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# モンキーパッチには二つの問題がある。 | |
# ・変更の範囲が「グローバル」である事。 | |
# ・変更が行われた事が「見えづらい」事。 | |
# | |
# 変更が見えづらい点への対応策として、ActiveSupportではモジュールを利用してモンキーパッチを明示的にしている。 | |
# rails/activesupport/lib/active_support/core_ext配下。 | |
# モジュールにメソッドを定義して、オープンクラスでインクルードする事で、#ancestors()などで確認する事が出来る。 | |
# もちろんこれだけでは、「グローバル」な変更への配慮は出来ていないが。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var port = 1337; | |
var obj = {}; | |
var server = http.createServer(function(req, res) { | |
var remoteAddress = req.connection.remoteAddress; | |
var header = {'Connection': 'close', 'Content-Length': 0}; | |
var key = req.url | |
switch (req.method) { | |
case 'POST': |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static VALUE | |
rb_str_each_line(argc, argv, str) | |
int argc; | |
VALUE *argv; | |
VALUE str; | |
{ | |
VALUE rs; | |
int newline; | |
char *p = RSTRING(str)->ptr, *pend = p + RSTRING(str)->len, *s; | |
char *ptr = p; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The stack I wrote for http://vimeo.com/26391171 | |
# Note that there is an intentional bug in the each method :) | |
class Stack | |
Node = Struct.new :data, :next | |
def push(data) | |
@head = Node.new data, @head | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
class Concern < ActiveRecord::Base | |
belongs_to :publication | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; *** REPL *** | |
(defun game-repl () | |
(let ((cmd (game-read))) | |
(unless (eq (car cmd) 'quit) | |
(game-print (game-eval cmd)) | |
(game-repl)))) | |
;; 入力に対して()を補い第二引数以降をシンボルとする | |
;; ex. walk east far => (WALK 'EAST 'FAR) | |
(defun game-read () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/ruby1.9 | |
#-*-encoding: utf-8-*- | |
class Env < Hash | |
def initialize(parms=[], args=[], outer=nil) | |
h = Hash[parms.zip(args)] | |
self.merge!(h) | |
@outer = outer | |
end | |
def find(key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 使い方 | |
;; 有向グラフ (graph->png "graph.dot" *wizard-nodes* *wizard-edges*) | |
;; 無向グラフ (ugraph->png "ugraph.dot" *wizard-nodes* *wizard-edges*) | |
(defparameter *wizard-nodes* '((living-room (you are in the living-room. | |
a wizrd is snoring loudly on the couch.)) | |
(garden (you are in a beautiful garden. | |
there is a well infront of you.)) | |
(attic (you are in the attic. | |
there is a giant welding torch in the corner.)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
require 'hashie' | |
require 'readline' | |
@env = Hashie::Mash.new(YAML.load_file("./world.yml")) | |
@allowed_commands = %w(look walk pickup inventory) | |
@location = :living_room | |
def look | |
res = [] |