Skip to content

Instantly share code, notes, and snippets.

View flada-auxv's full-sized avatar
🎯
Focusing

flada-auxv flada-auxv

🎯
Focusing
View GitHub Profile
@flada-auxv
flada-auxv / sort_by.rb
Created November 28, 2012 14:13
sort()とsort_by()と安定ソートについて
# -*- 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
@flada-auxv
flada-auxv / monky_patch.rb
Created December 2, 2012 15:38
モンキーパッチの影響と対応策。ActiveSupportに見られるモジュールを利用する手法とRuby2.0から導入されるrefinments
# -*- coding: utf-8 -*-
# モンキーパッチには二つの問題がある。
# ・変更の範囲が「グローバル」である事。
# ・変更が行われた事が「見えづらい」事。
#
# 変更が見えづらい点への対応策として、ActiveSupportではモジュールを利用してモンキーパッチを明示的にしている。
# rails/activesupport/lib/active_support/core_ext配下。
# モジュールにメソッドを定義して、オープンクラスでインクルードする事で、#ancestors()などで確認する事が出来る。
# もちろんこれだけでは、「グローバル」な変更への配慮は出来ていないが。
@flada-auxv
flada-auxv / http_server_restfuljson.js
Created December 3, 2012 14:06
[写経]Node.js入門10.3
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':
@flada-auxv
flada-auxv / string.c
Created December 4, 2012 02:00
pryからのテスト投稿
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;
@flada-auxv
flada-auxv / stack.rb
Created December 4, 2012 03:15 — forked from JoshCheek/stack.rb
The stack from my Pry screencast
# 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
# coding: utf-8
class Concern < ActiveRecord::Base
belongs_to :publication
end
@flada-auxv
flada-auxv / game_repl.lisp
Created March 12, 2013 16:12
Land of Lisp 第5章
;; *** 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 ()
#!/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)
@flada-auxv
flada-auxv / graph.lisp
Created March 16, 2013 16:43
Land of Lisp 第7章
;; 使い方
;; 有向グラフ (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.))))
@flada-auxv
flada-auxv / text_game.rb
Created March 17, 2013 12:48
usage追加してみた。一文字で打てるようにはしてない!
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 = []