原文来自: http://stackoverflow.com/questions/3236194/defining-method-called-how-do-i-make-a-hook-method-which-gets-called-every-t
I want to make a hook method which gets called everytime any function of a class gets called. I have tried method_added, but it executes only once at the time of class definition,
class Base
def self.method_added(name)
p "#{name.to_s.capitalize} Method's been called!!"
end
def a
p "a called."
end
def b
p "b called."
end
end
t1 = Base.new
t1.a
t1.b
t1.a
t1.b
Output:
"A Method's been called!!"
"B Method's been called!!"
"a called."
"b called."
"a called."
"b called."
but my requirement is that any function of a class that gets called anywhere in the program triggers the "method_called", hook method.
Expected Output:
"A Method's been called!!"
"a called."
"B Method's been called!!"
"b called."
"A Method's been called!!"
"a called."
"B Method's been called!!"
"b called."
If there is any defined existing hook method that works just the same, then please tell about it.
Thanks in advance..
Take a look at Kernel#set_trace_func. It lets you specify a proc which is invoked whenever an event (such as a method call) occurs.
class Base
def a
puts "in method a"
end
def b
puts "in method b"
end
end
set_trace_func proc { |event, file, line, id, binding, classname|
# only interested in events of type 'call' (Ruby method calls)
# see the docs for set_trace_func for other supported event types
puts "#{classname} #{id} called" if event == 'call'
}
b = Base.new
b.a
b.b
Outputs:
Base a called
in method a
Base b called
in method b
最佳答案:
class Base
def self.method_added(name)
if /hook/.match(name.to_s) or method_defined?("#{name}_without_hook")
return
end
hook = "def #{name}_hook\n p 'Method #{name} has been called'\n #{name}_without_hook\nend"
self.class_eval(hook)
a1 = "alias #{name}_without_hook #{name}"
self.class_eval(a1)
a2 = "alias #{name} #{name}_hook"
self.class_eval(a2)
end
def a
p "a called."
end
def b
p "b called."
end
end
t1 = Base.new
t1.a
t1.b
t1.a
t1.b
分享到:
相关推荐
"11.Defining_Terms.md" 提供了定义专业术语的策略,帮助读者理解文章的专有名词。 9. **比较与对比(10.Compare_and_Contrast.md)** 对比和对比分析能揭示事物之间的关系。"10.Compare_and_Contrast.md" 提供了...
which the Assign method is called. - ADD: In TIdPool (module FlexUtils), added the method NextUsed (allows iterating all taken identifiers). - ADD: Added the method TEnumProp.SetItem for convenience...
PART I: Presenting Flex CHAPTER 1: Introducing Flex. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 About Flex. . . . . . . . . . . . . . . . . . . . . . . . . . . ...
PART I GETTING STARTED. . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Installing and Configuring MySQL. . . . . . . . . . . . . . . . 3 Various MySQL Distributions. . . . . . . . . . ....
3G_Americas_Defining_4G_WP_July2007.pdf
- **Notation for Constraints:** Explains how constraints are represented mathematically, which is crucial for defining the feasible region of a design problem. - **Superscripts/Subscripts and ...
You can use Ant to compile and run Groovy scripts by defining a task that calls `groovyc` and `java`. This approach is useful for integrating Groovy scripts into larger build processes. #### Groovy ...
Define MPI_POINTER if it hasn t already been defined. By default MPI_POINTER is defined to be a near pointer. MPI_POINTER can be defined as a far pointer by defining MPI_POINTER as far before this ...
- **The First Class**: This involves defining a simple Java class that will be mapped to a database table. - **The Mapping File**: Here, you learn how to create an XML file that describes the mapping ...
##### 4.6 描述和定义类别 (Describing And Defining Classes) 描述和定义类别通常涉及使用属性限制来限定一个类别的成员。例如,可以定义“成年人”这个类别为“年龄大于18岁的人”。 ##### 4.7 使用推理器 (Using...
11.Defining Constant values......................................................................................................4 12.void argument........................................................
Coding of manipulator having 2 links for main function defining the functions accordingly.
How to do it: feedback link.........................27 Further reading...............27 Day 16: Not opening new windows....................28 Who benefits?.................28 How to do it.28 ...
How to do it... How it works... There's more... See also Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions 1. Pandas ...
The scheme operates by defining a common format for the encryption related metadata necessary to decrypt the protected streams, yet leaves the details of rights mappings, key acquisition and storage,...
wchar t is required in stdlib.h according to POSIX. note that defining need wchar t prevents stddef.h Source Code for Linux v2.13.6.
This demo implements a model of a lead-acid battery cell using the Simscape™ language to implement the nonlinear equations of the equivalent circuit components. In this way, as opposed to ...
- 创建简单约束(Creating a simple constraint)。 - 约束注解(The constraint annotation)。 - 约束验证器(The constraint validator)。 - 错误消息(The error message)。 - 使用约束(Using the ...