
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 super_at(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

  # TODO DEPRECATE
  alias superior super_at

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
