当增加新方法,类方法删除和取消定义的现有方法
class Tracker
def important
"This is an important method!"
end
def self.method_added(sym)
if sym == :important
raise 'The "important" method has been redefined!'
else
puts %{Method "#{sym}" was (re)defined.}
end
end
def self.method_removed(sym)
if sym == :important
raise 'The "important" method has been removed!'
else
puts %{Method "#{sym}" was removed.}
end
end
def self.method_undefined(sym)
if sym == :important
raise 'The "important" method has been undefined!'
else
puts %{Method "#{sym}" was removed.}
end
end
end
当模块混入一个类时
class Module
alias_method :include_no_hook, :include
def include(*modules)
# Run the old implementation.
include_no_hook(*modules)
# Then run the hook.
modules.each do |mod|
self.include_hook mod
end
end
def include_hook
# Do nothing by default, just like Module#method_added et al.
# This method must be overridden in a subclass to do something useful.
end
end
分享到:
相关推荐
"cookbook-zh-CN.md"文件很可能是这本书的主要文本部分,里面详细列举了各种使用PySimpleGUI的技巧和实践案例。每个章节通常会介绍一个特定的功能或概念,并配有相应的代码示例。通过阅读和实践这些例子,开发者可以...
Bioinformatics-with-Python-Cookbook-Learn-how-to-use-modern-Python-bioinformatics-libraries-and-applications-to-do-cutting-edge-research-in-computational-biology.pdf
《CMake Cookbook》是关于构建、测试和打包模块化软件的专业指南,专注于现代CMake工具的使用。本书由Radovan Bast和Roberto Di Remigio合著,旨在帮助读者掌握CMake这一强大的跨平台构建系统。 CMake是一个开源的...
This is a cookbook of problem-solving recipes about Scala, the most interesting pro‐ gramming language I’ve ever used. The book contains solutions to more than 250 com‐ mon problems, shown with ...
IPython-Interactive-Computing-and-Visualization-Cookbook-Over-100-hands-on-recipes-to-sharpen-your-skills-in-high-performance-numerical-computing-and-data-science-with-Python.pdf
Practical-Data-Science-Cookbook-89-hands-on-recipes-to-help-you-complete-real-world-data-science-projects-in-R-and-Python.pdf
资源分类:Python库 所属语言:Python 资源全名:slapos.cookbook-1.0.41.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
《Python库剖析:slapos.cookbook-0.75.0》 在Python的世界里,库是开发者的重要工具,它们提供了丰富的功能,使得代码编写更为高效和便捷。本篇文章将深入探讨名为“slapos.cookbook”的Python库,版本为0.75.0,...
practicing-ruby-cookbook, practicingruby.com的主厨 cookbook ruby-cookbook这个 Chef cookbook 完全自动化了设置环境的过程,该环境可以运行实践 ruby Rails 应用程序。 在实践 ruby的时候,可以以将一个简单的...
Android-Android-Cookbook-Examples.zip,O'Reilly Android Cookbook提供的代码示例。请参阅下面的用户内容表自述文件!,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux...
The FortiGate Cookbook 5.0.4 (Expanded Version) Essential Recipes for Success with your FortiGate September 25, 2013 Copyright© 2013 Fortinet, Inc. All rights reserved. Fortinet®, FortiGate®, and ...
This book is intended for system administrators who have some experience with VMware ESX, ESXi, vCloud Director, or ...only tried to appeal to beginners, but we also include a generous amount of complex
Lott -- Modern Python Cookbook -- 2016 -- code.7z
Aggarwal -- Flask Framework Cookbook -- 2014 -- code.7z
Subramanian -- Python Data Science Cookbook -- 2015 -- code.7z
Fine -- Python 2.6 Graphics Cookbook -- 2010 -- code.7z