Created
January 20, 2012 16:51
-
-
Save jacaetevha/1648356 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Annotations | |
| def override | |
| @check_method = true | |
| end | |
| def singleton_method_added method_name | |
| if @check_method | |
| clazz = self.ancestors.reject{|e| e == self}.first | |
| unless clazz.methods.include? method_name | |
| raise "None of #{self}'s ancestors defines ##{method_name}" | |
| end | |
| super_method = clazz.method method_name | |
| my_method = self.method method_name | |
| unless super_method.arity == my_method.arity | |
| raise "Arity Mismatch: #{clazz}>>##{method_name}(#{super_method.arity}) vs. #{self}>>##{method_name}(#{my_method.arity})" | |
| end | |
| end | |
| @check_method = false | |
| end | |
| def method_added method_name | |
| if @check_method | |
| clazz = self.ancestors.reject{|e| e == self}.first | |
| unless clazz.instance_methods.include? method_name | |
| raise "None of #{self}'s ancestors defines ##{method_name}" | |
| end | |
| super_method = clazz.instance_method method_name | |
| my_method = self.instance_method method_name | |
| unless super_method.arity == my_method.arity | |
| raise "Arity Mismatch: #{clazz}##{method_name}(#{super_method.arity}) vs. #{self}##{method_name}(#{my_method.arity})" | |
| end | |
| end | |
| @check_method = nil | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ! ruby try-annotations.rb | |
| Example B: | |
| Arity Mismatch: A>>#with(1) vs. B>>#with(0) | |
| Example C: | |
| None of C's ancestors defines #not | |
| Example D: | |
| None of D's ancestors defines #two | |
| Example E: | |
| Arity Mismatch: A#number(0) vs. E#number(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $:.unshift '.' | |
| require 'annotate' | |
| class A | |
| def initialize num | |
| @num = num | |
| end | |
| def number | |
| @num | |
| end | |
| def self.with num | |
| self.new num | |
| end | |
| end | |
| begin | |
| class B < A | |
| extend Annotations | |
| override | |
| def self.with | |
| self.new [1,2] | |
| end | |
| end | |
| rescue | |
| puts "Example B:\n#{$!.message}\n\n" | |
| end | |
| begin | |
| class C < A | |
| extend Annotations | |
| override | |
| def self.not num | |
| self.new num | |
| end | |
| end | |
| rescue | |
| puts "Example C:\n#{$!.message}\n\n" | |
| end | |
| begin | |
| class D < A | |
| extend Annotations | |
| def initialize | |
| super 2 | |
| end | |
| def one | |
| 1 | |
| end | |
| override | |
| def two | |
| 2 | |
| end | |
| end | |
| rescue | |
| puts "Example D:\n#{$!.message}\n\n" | |
| end | |
| begin | |
| class E < A | |
| extend Annotations | |
| override | |
| def number num | |
| num | |
| end | |
| end | |
| rescue | |
| puts "Example E:\n#{$!.message}\n\n" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment