This file contains hidden or 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
| 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 |
This file contains hidden or 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 Tapped < BasicObject | |
| private *instance_methods # 全メソッドをmethod_missingへ転送する | |
| def initialize obj, result=self | |
| @obj = obj | |
| @result = result | |
| end |
This file contains hidden or 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
| /* イメージを書き留める | |
| * 実際には動きません | |
| */ | |
| void (**func)(); // 関数ポインタへのポインタを用意する | |
| (*func)() // 呼び出し | |
| // funcの値は不変なので、 |
This file contains hidden or 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
| class Count(object): | |
| def __init__(self): | |
| self.count = 0 | |
| def up(self): | |
| try: | |
| return self.count | |
| finally: |
This file contains hidden or 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 -*- | |
| # Rubyだったら正規表現はすごい簡単だよ!!! | |
| # 他の言語ではいくつかパターンがありますが、自分はRubyほど楽に正規表現を利用できる言語を知りません。 | |
| msg = "まなさん大好き" | |
| # Ruby では正規表現リテラルがあります。 | |
| # // のリテラルはそれだけで正規表現のリテラルです | |
| # かなりperl的なのです^q^ |
This file contains hidden or 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 -*- | |
| # Ruby では動的プロクシオブジェクトがよく使われます。 | |
| # 利用例は | |
| # - https://gist.github.com/1436624 | |
| # - https://gist.github.com/1263663 | |
| # - https://github.com/pasberth/Dam/blob/master/src/dam.rb | |
| # gems では lazy などが使っています。 | |
| # ============================================================ | |
| # | |
| # ぼくは動的プロクシをダックタイピングの代表だと思っています。 |
This file contains hidden or 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
| module NormallyCase; extend self; end | |
| module NormallyCase | |
| def count_up | |
| @count ||= 0 | |
| @count += 1 | |
| end |
This file contains hidden or 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
| # 特異メソッドの構文 | |
| # def #{self}.singleton_method | |
| # で特異メソッドを追加する | |
| # $ irb | |
| Mana = Object.new | |
| # => #<Object:0x007fef1a985970> | |
| Mana | |
| # => #<Object:0x007fef1a985970> | |
| def Mana.to_s | |
| "角田愛実" |
This file contains hidden or 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
| # 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| |
This file contains hidden or 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
| 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 } |