Skip to content

Instantly share code, notes, and snippets.

View pasberth's full-sized avatar

pasberth pasberth

View GitHub Profile
@pasberth
pasberth / object.rb
Created December 2, 2011 05:50
謎的なおぶじぇくと
def object constant=nil, &defi
obj = ->() do
b = binding
return ->(exp) do
b.eval exp
end
end.call
->() do
if constant.respond_to? :to_sym
@pasberth
pasberth / tap_ext.rb
Created December 6, 2011 03:47
Object#tapはこう動いて欲しい!!!!!!
# -*- coding: utf-8 -*-
class Tapped < BasicObject
private *instance_methods # 全メソッドをmethod_missingへ転送する
def initialize obj, result=self
@obj = obj
@result = result
end
@pasberth
pasberth / overrid.c
Created December 7, 2011 16:21
いめーじをかきとめ
/* イメージを書き留める
* 実際には動きません
*/
void (**func)(); // 関数ポインタへのポインタを用意する
(*func)() // 呼び出し
// funcの値は不変なので、
class Count(object):
def __init__(self):
self.count = 0
def up(self):
try:
return self.count
finally:
@pasberth
pasberth / easy_regexp.rb
Created December 10, 2011 15:57
Rubyだったらこんなに楽に正規表現を書けるよ!!
# -*- coding: utf-8 -*-
# Rubyだったら正規表現はすごい簡単だよ!!!
# 他の言語ではいくつかパターンがありますが、自分はRubyほど楽に正規表現を利用できる言語を知りません。
msg = "まなさん大好き"
# Ruby では正規表現リテラルがあります。
# // のリテラルはそれだけで正規表現のリテラルです
# かなりperl的なのです^q^
# -*- coding: utf-8 -*-
# Ruby では動的プロクシオブジェクトがよく使われます。
# 利用例は
# - https://gist.github.com/1436624
# - https://gist.github.com/1263663
# - https://github.com/pasberth/Dam/blob/master/src/dam.rb
# gems では lazy などが使っています。
# ============================================================
#
# ぼくは動的プロクシをダックタイピングの代表だと思っています。
module NormallyCase; extend self; end
module NormallyCase
def count_up
@count ||= 0
@count += 1
end
@pasberth
pasberth / to_sとinspect.rb
Created December 11, 2011 03:59
特異メソッド(singleton method)について。
# 特異メソッドの構文
# def #{self}.singleton_method
# で特異メソッドを追加する
# $ irb
Mana = Object.new
# => #<Object:0x007fef1a985970>
Mana
# => #<Object:0x007fef1a985970>
def Mana.to_s
"角田愛実"
@pasberth
pasberth / hello.rb
Created December 12, 2011 03:37
ごく一般的なRubyによるhello world
# puts "hello world"
# 表のrubyで伝承されるhello worldは↑になる
# しかし裏のrubyで秘伝されるhello worldはこうなるッッッ!!
Object.new.instance_eval do
define_singleton_method :to_s do "hello world" end
Module.new do
define_singleton_method :extended do |sub|
@pasberth
pasberth / checkout.rb
Created December 12, 2011 15:35
べつのクラスを一時的に同じインスタンスのように振る舞わせる
class Object
alias checkout_original_method_missing method_missing
def checkout! into
into = case into.class
when Class then into.new
when Module then Object.new.tap { |i| i.extend into }