Sha256: 47f5c6ae2a4d80f631a76087d8c9b3bb8b4d1a78a9dffa0b57964ccbb6bf0416

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

class Pervasives
  VERSION = "0.0.1"

  METHODS = Hash.new{|h,k| h[k] = {}}

  [Module, Class, Object].each do |type|
    type.instance_methods.each do |mname|
      m = type.instance_method mname
      METHODS[type][mname.to_s] = m
    end
  end

  class << self
    mnames = METHODS[Module].keys + METHODS[Class].keys + METHODS[Object].keys 

    mnames.each do |mname|
      next if mname['__']
      module_eval <<-code
        def #{ mname } obj, *argv, &block
          search_path = case obj
            when Class
              [Class, Module, Object]
            when Module
              [Module, Object]
            else
              [Object]
          end
          type = search_path.detect{|type| METHODS[type]['#{ mname }']} 
          m = METHODS[type]['#{ mname }']
          m.bind( obj ).call( *argv, &block )
        end
      code
    end
  end

  class Proxy
    alias_method "__class__", "class"
    alias_method "__instance_eval__", "instance_eval"
    instance_methods.each{|m| undef_method m unless m[%r/__/]}
    def initialize obj
      @obj = obj
    end
    def method_missing m, *a, &b
      Pervasives.__send__ m, @obj, *a, &b
    end
  end
end




module Kernel 
  def __ obj, &b
    proxy = Pervasives::Proxy.new obj
    b ? proxy.__instance_eval__(&b) : proxy
  end
  def __! obj, &b
    proxy = Pervasives::Proxy.new obj
    b ? proxy.__instance_eval__(&b) : proxy
  end
end

Version data entries

2 entries across 1 versions & 1 rubygems

Version Path
pervasives-0.0.1 lib/pervasives-0.0.1.rb
pervasives-0.0.1 lib/pervasives.rb