Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Integer
- def random_string
- (1..self).collect { (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join
- end
- end
- class Module
- def wrap method, &block
- term = "_" + 5.random_string
- unless method_defined? "#{method}#{term}"
- alias_method "#{method}#{term}", method
- else
- return override(method, &block)
- end
- define_method(method) do |*args|
- begin
- block[*args]
- rescue
- self.send("#{method}#{term}", *args)
- end
- end
- public(method)
- end
- end
- class File
- wrap(:atime) do |*args|
- raise "Blah" unless args[0] == 0
- 42
- end
- wrap(:atime) do |*args|
- raise "Blah" unless args[0] == 1
- nil
- end
- end
- File.new("a").atime # => Wed Oct 18 15:23:36 PDT 2006
- File.new("a").atime(0) # => 42
- File.new("a").atime(0) == 42 # => true
- File.new("a").atime(1) # => nil
- !File.new("a").atime(1) # => true
Add Comment
Please, Sign In to add comment