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