`
xuchunming
  • 浏览: 30434 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

code-snipplet|| BeforeAndAfterMethod

阅读更多
require "before_and_after"

class Message
  include BeforeAndAfter

  def initialize message
    @message = message
  end

  def display
    puts @message
  end

  def before_display
    puts "BEFORE DISPLAY"
  end

  def after_display
    puts "AFTER DISPLAY"
  end

  use_method :display
end

Message.new( "== MESSAGE ==" ).display



BEFORE DISPLAY
== MESSAGE ==
AFTER DISPLAY



  
  module BeforeAndAfter
  # This extends the class that includes BeforeAndAfter with the methods in ClassMethods
  def self.included(base)
    base.extend(ClassMethods)
  end
  
  module ClassMethods
    def use_method *methods
      methods.each { |method|
        # Set up the before and after variables
        before_method = "before_#{method.to_s}".to_sym
        after_method = "after_#{method.to_s}".to_sym
        # Unbind the original, before, and after methods
        unbinded_before_method = instance_method( before_method )
        unbinded_method = instance_method( method )
        unbinded_after_method = instance_method( after_method )
        # Define the before and after methods if they don't already exist
        define_method( before_method ) unless self.method_defined?( before_method )
        define_method( after_method ) unless self.method_defined?( after_method )
        # Redefines the method to run the before and after methods
        define_method( method ) {
          unbinded_before_method.bind( self ).call # Bind the unbinded BEFORE method
          unbinded_method.bind( self ).call        # Bind the original method
          unbinded_after_method.bind( self ).call  # Bind the unbinded AFTER method
        }
      }
    end
  end
end



http://snipplr.com/view/17292/beforeandafter/
分享到:
评论

相关推荐

    Java 学生信息管理系统-期末大作业

    Java 学生信息管理系统-期末大作业 # sms接口文档-V1.0 ## 1.... ### 1.1 获取学生详细信息 ...| code | number | 必须 | | 响应码, 0-成功,1-失败 | | | message | string | 非必须 | | 提示信息

    java基于Spring Cloud的网约车项目

    | 模块 | 项目名 | | ------------ | ------------------------- | | 订单 | service-order ...| 验证码 | service-verification-code | | 钱包 | service-wallet | | 支付 | service-payment | | 地图 | service-map |

    U9集成 API帮助手册

    | Code | Code | 编码 | 物料的编码 | | Name | Name | 名称 | 物料的名称 | | AliasName | Alias Name | 别名 | 物料的别名 | | Specs | Specs | 规格 | 物料的规格 | | CatalogNO | Catalog NO | 目录编号 | 物料...

    具有后台管理系统、基于wechat-python-sdk和flask的微信公众号面基机器人+源代码+文档说明

    | code | 统计汇总识别码-划分代码 | - | | name | 省/直辖市/自治区名称 | - | | url | 下级链接地址 | 搜索二级时需要 | | searched | 是否搜索过下级链接地址 | 搜索二级时需要 | ### 表 city (二级:地级市) | ...

    php汉字转拼音

    function Pinyin($_String, $_Code='gb2312') { $_DataKey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha". "|chai|chan|chang|chao|che|...

    cute http file server 开发API

    | HTTPCode | 描述 | 数据示例 | |----------|------------------|-------------------------| | 500 | 服务器内部错误 | `{error: "xxxxxx"}` | | 201 | 成功 | `{token: "xxxxxx"}` `Cookie: "JWT"+"user"` | |...

    通用App接口定义文档规范样本.doc

    这些接口都是基于JSON格式的数据交换,返回数据格式为{code:’0000’,msg:’’,data:{}}。 1. 通用响应码表 在通用App接口定义中,使用了响应码表来表示不同的响应结果。响应码表包括: * SUCCESS = "0000",表示...

    学生选课管理信息系统设计与实现.doc

    | Usercode | Char | 16 | | Y | 登录密码 | | Usertype | Char | 10 | | Y | 类型 | 四、系统界面设计 系统界面设计中,包括登录界面、学生信息管理模块、课程信息管理模块、用户管理模块、学生选课模块、成绩...

    usb 键盘 单片机

    Key.c 2225 22-03-07|16:25 Key.h 706 22-03-07|15:58 Key.LST 4569 23-03-07|23:58 Key.OBJ 5258 23-03-07|23:58 keyboard.h 2102 22-03-07|16:02 ...SourceCode 0 07-08-08|00:37 USB键盘 0 07-08-08|00:37

    泰文的显示规则及组字方式

    在MS-DOS系统中,必须具备的辅助字符集是Code Page 437。 #### 图1-1:基本字符集(TACTIS代码) 表1-1展示了基本字符集(TACTIS代码)的具体内容,包括控制字符和部分可打印字符。表中详细列出了十六进制范围内的...

    用于抽取摘要的纸张微调BERT代码.zip

    用于抽取摘要的纸张微调BERT代码# BertSum**This code is for paper `Fine-tune BERT for Extractive Summarization`**(https://arxiv.org/pdf/1903.10318.pdf)**!New: Please see our [full paper]...

    图书系统数据库表.docx

    | 4 | CodeDesc | 类型说明 | varchar | | | | 5 | IsValid | 是否启用 | varchar | | | | 6 | Remark | 备注 | varchar | | | | 7 | AddDate | 添加日期 | datetime| | | **说明**:编码表用于存储系统中各种编码...

    sap t-code and menu relationship

    and t-code's relationship. SAP standard menu | |-- Office |-- Logistics | | | |-- Materials management | | | | | |-- Inventory management | | | | | | | |-- Goods movement | | | | | | | | ...

    手机内置天线案例

    本篇文章将通过几个具体的内置天线案例来探讨不同类型的天线在CDMA(Code Division Multiple Access,码分多址)和GSM(Global System for Mobile Communications,全球移动通信系统)频段上的性能表现。通过这些...

    ios选择类型

    | code | 验证码(可选) | String | | message| 成功或失败消息 | String | **参数示例** - 输入参数示例 ```json { "phone": "+1234567890" } ``` - 输出参数示例 ```json { "status": "success", "code...

    UL68X 友顺UTC 电子元器件芯片.pdf

    | PACKAGE VOLTAGE CODE | MARKING | | --- | --- | | 25 | 250mV | | 60 | 600mV | UL68X 是一款高精度的恒流 IC,广泛应用于 LED 驱动器、电池充电器、电源管理等领域。其简单的应用方案和低廉的成本使其在电子元...

    Linux内核驱动详解

    linuxdriver_code_tool |-- 03 | `-- 2.6内核升级工具 | |-- device-mapper-1.00.19-2.i386.rpm | |-- lvm2-2.00.25-1.01.i386.rpm | |-- mkinitrd-4.2.0.3.tar.tar | |-- module-init-tools-3.2.2.tar.bz2 | `-- ...

    研华 板卡 增益码 gaincode

    ### 研华板卡增益码(Gain Code)详解 #### 一、引言 在数据采集系统中,为了确保信号的精确性和可靠性,往往需要对输入信号进行增益调整。增益码(Gain Code)是配置板卡增益的重要参数之一。本文将详细介绍研华...

    S3C2410各种驱动源码

    linuxdriver_code_tool |-- 03 | `-- 2.6内核升级工具 | |-- device-mapper-1.00.19-2.i386.rpm | |-- lvm2-2.00.25-1.01.i386.rpm | |-- mkinitrd-4.2.0.3.tar.tar | |-- module-init-tools-3.2.2.tar.bz2 | `-- ...

    IDK02G65C5 英飞凌芯片 INFINEON 中文版规格书手册.pdf

    | 引脚 | 引脚名称 | 类型 | ordering Code | 包装 | 标记 | | --- | --- | --- | --- | --- | --- | | 1 | Pin 1 | C | | PG-TO263-2 | D0265C5 | | 2 | Pin 2 | A | | | | | 3 | Pin 3 | n.a. | | | | 该产品的...

Global site tag (gtag.js) - Google Analytics