Skip to content

Instantly share code, notes, and snippets.

@osyo-manga
osyo-manga / gist-file0.txt
Created November 24, 2015 04:11
call_once
def call_one &block
flag = false
proc { flag || instance_eval(&block); flag = true }
end
class X
define_method(:initialize, &call_one do
p "homu"
end)
@osyo-manga
osyo-manga / type.md
Created October 30, 2015 19:24
typed

Ruby の型

やりたいこと

  • Ruby で型を定義する
  • 現状は Class = 型とする
  • 型チェックを行う仕組み
  • ジェネリックプログラミングや Variant 型(Haskell の Either みたいなの?)の定義
  • C++ の template みたいなのもほしい
  • 型によって処理を切り替える装置
class Module
def sticher_accessor **opt
opt.each { |name, type|
if type.class == Class
# value = type.new
value = nil
else
value = type
type = type.class
end
@osyo-manga
osyo-manga / main.rb
Last active October 25, 2015 15:44
ruby typed
def let **opt, &block
Object.new.instance_eval do
for name, type in opt
if type.class == Class
value = type.new
else
tyep = type.class
value = type
end
@osyo-manga
osyo-manga / matome.md
Created October 23, 2015 07:46
stitcher

Stitcher

目的

  • メソッドの多重定義をする
  • 引数によって呼び出すメソッドを切り替える
  • メソッドの引数に対しての制限を設ける
  • 引数に対する型チェック
  • 引数に対するコンセプトチェック
  • モンキーパッチで定義するメソッドを制限したい
@osyo-manga
osyo-manga / main.rb
Created October 18, 2015 10:12
concept
class X
def self.concept name, &request
impl_name = "#{name}_#{request}"
alias_method impl_name, name
@@concept_methods ||= {}
@@concept_methods[name] ||= []
@@concept_methods[name].unshift [request, impl_name]
define_method(name){ |*args|
for request, name_ in @@concept_methods[name]
## 材料
* 玉ねぎ(半分をみじん切り)
* にんにく(好みで)
* コンソメ1つ(お湯で溶かしておく)
* お湯(やかん等で沸かしておく)
* 生米(2合、洗わない)
* 胡椒
* オリーブオイル
@osyo-manga
osyo-manga / main.rb
Created October 9, 2015 04:45
method
module MethodWrapper
def wrap name, &block
prepend(Module.new do
define_method name do
super_ = proc { super() }
Object.new.instance_eval do
define_singleton_method :super_ do
super_.()
end
instance_eval &block
class Base
def homu
p :base
end
end
class Derived < Base
def super
p :super
end
module MethodWrapper
def wrap name, &block
prepend(Module.new do
define_method name do
super_ = proc { super() }
Object.new.instance_eval do
define_singleton_method :super_ do
super_.()
end
instance_eval &block