Sha256: 015f306bbdc470cf42bca11f13645b66ebff1a9e2e596b7855a95b3495e7e11f

Contents?: true

Size: 966 Bytes

Versions: 20

Compression:

Stored size: 966 Bytes

Contents

class Object

  # Like super but skips to a specific ancestor module or class.
  #
  #   class A
  #     def x ; 1 ; end
  #   end
  #
  #   class B < A
  #     def x ; 2 ; end
  #   end
  #
  #   class C < B
  #     def x ; superior(A) ; end
  #   end
  #
  #   C.new.x  #=> 1
  #
  def superior(klass=self.class.superclass, *args, &blk)
    unless self.class.ancestors.include?(klass)
      raise ArgumentError
    end
    called = /\`([^\']+)\'/.match(caller(1).first)[1].to_sym
    klass.instance_method(called).bind(self).call(*args,&blk)
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCKernel < Test::Unit::TestCase
    class X ; def x ; 1 ; end ; end
    class Y < X ; def x ; 2 ; end ; end
    class Z < Y ; def x ; p 'here'; superior(X) ; end ; end

    def test_superior
      z = Z.new
      assert_equal( 1, z.x )
    end

  end

=end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
facets-1.0.3 packages/core/lib/facet/kernel/superior.rb
facets-0.9.0 lib/nano/kernel/superior.rb
facets-1.0.0 lib/facet/kernel/superior.rb
facets-1.3.0 lib/facets/core/kernel/superior.rb
facets-1.1.0 lib/facet/kernel/superior.rb
facets-1.2.0 lib/facets/core/kernel/superior.rb
facets-1.2.1 lib/facets/core/kernel/superior.rb
facets-1.3.2 lib/facets/core/kernel/superior.rb
facets-1.3.1 lib/facets/core/kernel/superior.rb
facets-1.3.3 lib/facets/core/kernel/superior.rb
facets-1.4.1 lib/facets/core/kernel/superior.rb
facets-1.4.0 lib/facets/core/kernel/superior.rb
facets-1.4.2 lib/facets/core/kernel/superior.rb
facets-1.4.3 lib/facets/core/kernel/superior.rb
facets-1.4.4 lib/facets/core/kernel/superior.rb
facets-1.4.5 lib/facets/core/kernel/superior.rb
facets-1.7.0 lib/facets/core/kernel/superior.rb
facets-1.7.30 lib/facets/core/kernel/superior.rb
facets-1.7.38 lib/facets/core/kernel/superior.rb
facets-1.7.46 lib/facets/core/kernel/superior.rb