`
yanzilee9292
  • 浏览: 537313 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Defining “method_called”.. How do I make a hook method which gets called every t

    博客分类:
  • ruby
 
阅读更多

原文来自: 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
 
分享到:
评论

相关推荐

    Academic_Phrasebank_Markdown.zip

    "11.Defining_Terms.md" 提供了定义专业术语的策略,帮助读者理解文章的专有名词。 9. **比较与对比(10.Compare_and_Contrast.md)** 对比和对比分析能揭示事物之间的关系。"10.Compare_and_Contrast.md" 提供了...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    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...

    Developing Flex Applications 910p dda_doc88_cracker.zip

    PART I: Presenting Flex CHAPTER 1: Introducing Flex. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 About Flex. . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    php_6_fast_and_easy_web_development.pdf

    PART I GETTING STARTED. . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Installing and Configuring MySQL. . . . . . . . . . . . . . . . 3 Various MySQL Distributions. . . . . . . . . . ....

    3G_Americas_Defining_4G_WP_July2007.pdf

    3G_Americas_Defining_4G_WP_July2007.pdf

    Introduction_to_Optimum_Design.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 ...

    groovy_in_action_draft_ch_01.pdf

    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 ...

    mpi_type.rar_Before

    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 ...

    hibernate_reference.pdf

    - **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 ...

    Protege OWL Tutorial 官方教程

    ##### 4.6 描述和定义类别 (Describing And Defining Classes) 描述和定义类别通常涉及使用属性限制来限定一个类别的成员。例如,可以定义“成年人”这个类别为“年龄大于18岁的人”。 ##### 4.7 使用推理器 (Using...

    spice_style.pdf

    11.Defining Constant values......................................................................................................4 12.void argument........................................................

    Mani-Two_Link_Serial.zip_Links_coding_matlab

    Coding of manipulator having 2 links for main function defining the functions accordingly.

    30天打造更亲和力网站.rar

    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 ...

    Pandas Cookbook 2017 pdf 2分

    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 ...

    ISO_IEC_23001-7_2011.pdf

    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,...

    msm_mdp.rar_V2

    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.

    ssc_lead_acid_battery.zip_Battery model_lead acid battery_lead a

    This demo implements a model of a lead-acid battery cell using the Simscape&#8482 language to implement the nonlinear equations of the equivalent circuit components. In this way, as opposed to ...

    hibernate_validator_reference_5.1.3.pdf

    - 创建简单约束(Creating a simple constraint)。 - 约束注解(The constraint annotation)。 - 约束验证器(The constraint validator)。 - 错误消息(The error message)。 - 使用约束(Using the ...

Global site tag (gtag.js) - Google Analytics