方法一: 修改模块的include方法

module Bbq
def self.included(base)
base.send :include, InstanceMethods
base.extend ClassMethods
end module InstanceMethods
def m1
'instance method'
end
end module ClassMethods
def m2
'this is class method'
end
end
end class Test
include Bbq
end  

测试:

irb(main):030:0> Test.m2
=> "this is class method" irb(main):031:0> Test.m1
Traceback (most recent call last):
NoMethodError (undefined method `m1' for Test:Class) irb(main):032:0> Test.new.m1
=> "instance method"

  

方法二:借助ActiveSupport::Concern

require 'active_support/concern'

module Bbq2  extend ActiveSupport::Concern

  def m1
'instance method'
end class_methods do
def m2
'this is class method'
end
end
end class Test2
include Bbq2
end  

  

测试:

irb(main):019:0> Test2.m2
=> "this is class method"
irb(main):020:0> Test2.m1
Traceback (most recent call last):
NoMethodError (undefined method `m1' for Test2:Class)
irb(main):021:0> Test2.new.m1
=> "instance method"

  

[ROR] 如何在mixin模块中定义类方法(Howto define class methods in a mixin module)的更多相关文章

  1. 如何在nlp问题中定义自己的数据集

    我之前大致写了一篇在pytorch中如何自己定义数据集合,在这里如何自定义数据集 不过这个例子使用的是image,也就是图像.如果我们用到的是文本呢,处理的是NLP问题呢? 在解决这个问题的时候,我在 ...

  2. 在Angular中定义共享的Providers

    转自:https://segmentfault.com/a/1190000010700308 有时,你需要在 Angular 应用中创建一个共享模块,该模块定义了功能模块和lazy-loaded模块可 ...

  3. python 定义类方法

    定义类方法 和属性类似,方法也分实例方法和类方法. 在class中定义的全部是实例方法,实例方法第一个参数 self 是实例本身. 要在class中定义类方法,需要这么写: class Person( ...

  4. 如何在ALV_Grid的函数中定义下拉列表

    转自 http://www.cnblogs.com/VerySky/articles/2392262.htmlABAP--如何在ALV_Grid的函数中定义下拉列表 REPORT. ********* ...

  5. requirejs 定义模块中含有prototype

    因为我对requirejs不熟悉,不清楚如何定义带有prototype的模块, 在看了:https://gist.github.com/jonnyreeves/2474026 的demo之后,就明白了 ...

  6. Python中模块的定义及案例

    1 a = '我是模块中的变量a' 2 3 def hi(): 4 a = '我是函数里的变量a' 5 print('函数"hi"已经运行!') 6 7 class Go2: 8 ...

  7. 如何在WPF中定义窗体模板

    参考网址:https://www.cnblogs.com/chenxizhang/archive/2010/01/10/1643676.html可以在app.xaml中定义一个ControlTempl ...

  8. 如何在 ETL 项目中统一管理上百个 SSIS 包的日志和包配置框架

    一直准备写这么一篇有关 SSIS 日志系统的文章,但是发现很难一次写的很完整.因为这篇文章的内容可扩展的性太强,每多扩展一部分就意味着需要更多代码,示例和理论支撑.因此,我选择我觉得比较通用的 LOG ...

  9. 如何在NodeJS项目中优雅的使用ES6

    如何在NodeJS项目中优雅的使用ES6 NodeJs最近的版本都开始支持ES6(ES2015)的新特性了,设置已经支持了async/await这样的更高级的特性.只是在使用的时候需要在node后面加 ...

随机推荐

  1. 使用Supervisor管理Django应用进程

    官方文档 1.安装 pip install supervisor 2.使用说明 2.1  查看默认配置 echo_supervisord_conf 一般情况下,不需要去修改默认配置,而是将默认配置重定 ...

  2. l获取list 的长度,EL表达式获取list的长度,EL表达式获取list大小

    在jsp页面中不能通过${list.size}取列表长度,而是 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pref ...

  3. Python基础之输出格式和If判断

    格式化输出的三种方式 一.占位符 #占位符 name = 'nick' age = 19 print('my name is %s my age is %s' % (name, age)) age = ...

  4. Generator生成器函数执行过程的理解

    一个最基本的Generator函数格式如下,函数体内部要使用yield修饰符则必须在函数名前加上*号 ; function *testYield(x){ console.log('before yie ...

  5. UDF——监测指定点的物理量

    Fluent版本:2019 R1 Visual Studio版本:Visual Studio 2013 其他版本应该也是适用的 算例来源于:https://confluence.cornell.edu ...

  6. zooKeeper使用记录

    背景:记录zooKeeper使用过程中遇到的问题. 在删除zooKeeper相关节点的时候需要进行权限的认证,下面的连接讲的还是很详细的 zookeeper的ACL权限控制

  7. Source Insight添加新的文件类型

    1.前言 Source Insight这个软件工具功能非常强大,很适合用来分析一些大型的code工程,例如Linux内核源码,本文将简单介绍如何在Source Insight工程中添加一种新的文件类型 ...

  8. Web Api 实现删除功能接口

    删除数据 [HttpDelete] public ResultModel DeleteDataById(int id) { var result = new ResultModel(); //实例化数 ...

  9. java -- eclipse运行javaweb 项目

    这个是和上一个放在一块的 创建javaweb项目,要是想要创建maven项目,java项目都可以,我要和tomcat放在一块所以 就创建javaweb项目 创建项目起一个有意义的项目名    选择一个 ...

  10. Prometheus 标签使用示例整合

    Prometheus 监控实例 一.Prometheus 根据标签聚合总CPU使用率 1.主机添加标签(可在多个主机内添加相同标签实现聚合):vim prometheus.conf static_co ...