Sha256: 9f7d5be93289ed8475ce344000aa0c53f25eee5cda087d8d33f1f5f220838a11

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

module Kernel

  module Pervasives
    METHODS = {}
    Kernel.instance_methods.each do |m|
      METHODS[m] = Kernel.instance_method(m)
    end
    METHODS.each do |s,m|
      define_method s do |o,*a|
        m.bind(o).call(*a)
      end
      module_function s
    end
  end
  Pervasives.freeze(Pervasives)

  def >>(a)
    a = [a].flatten
    case a[0] when Module, Class
      c, m, *a = *a
      c.instance_method(m).bind(self).call(*a)
    else
      if respond_to?(a[0])
        Pervasives.send(self,*a)
      elsif respond_to?(:method_missing)
        method_missing(*a)
      else
        raise NoMethodError, "private method called for #{self}"
      end

    end
  end

end



=begin test

  require 'test/unit'

  class TestSendOP < Test::Unit::TestCase

    class C
      def a; "c"; end
    end

    class X < C
      def send; "send"; end
      def a; "a"; end
      private
      def b; "b"; end
    end

    def setup
      @x = X.new
    end

    def test_send
      assert_equal( "send", @x.send )
    end

    def test_normal
      assert_equal( "a", @x >> [:a] )
    end

    def test_as_C
      assert_equal( "c", @x >> [C,:a] )
    end

    def test_singleton
      assert_equal( "a", @x >> [(class<<@x;self;end),:a] )
    end

    def test_singleton_bypass_private
      assert_equal( "b", @x >> [(class<<@x;self;end),:b] )
    end

    def test_private
      assert_raises(NoMethodError){ @x >> [:b] }
    end

  end

=end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-1.8.0 work/op_rshift.rb
facets-1.8.20 work/op_rshift.rb
facets-1.8.49 work/op_rshift.rb
facets-1.8.8 work/op_rshift.rb