`
sg552
  • 浏览: 612992 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
47437293-94b9-3b04-9152-8971c7580049
白手起家学习使用Flex
浏览量:18441
社区版块
存档分类
最新评论

RUBY中重新定义常量(redefine constants in Ruby)

阅读更多
有个需求:    生产环境中的代码, 有一个常量。
但是需要在测试环境中修改它,把它从一个真正的对象变成mock object.

(提示:核心方法:  const_set, const_defined? , remove_const )

所以搜索了一下,有这个文章:
( http://stackoverflow.com/a/3377188/445908 )

先定义这个module
module RemovableConstants

  def def_if_not_defined(const, value)
    self.class.const_set(const, value) unless self.class.const_defined?(const)
  end

  def redef_without_warning(const, value)
    self.class.send(:remove_const, const) if self.class.const_defined?(const)
    self.class.const_set(const, value)
  end
end

然后就可以调用它了:
class A
  include RemovableConstants

  def initialize
    def_if_not_defined("Foo", "ABC")
    def_if_not_defined("Bar", "DEF")
  end

  def show_constants
    puts "Foo is #{Foo}"
    puts "Bar is #{Bar}"
  end

  def reload
    redef_without_warning("Foo", "GHI")
    redef_without_warning("Bar", "JKL")
  end

end

a = A.new
a.show_constants
a.reload
a.show_constants


运行结果:
引用
Foo is ABC
Bar is DEF
Foo is GHI
Bar is JKL


下面是我自己的代码:

class Apple
  COLOR = "red"
end

puts Apple::COLOR
puts "const_defined?  #{Apple.const_defined?("COLOR")}"
Apple.send(:remove_const, 'COLOR')

# 三种不同的方法,都是定义常量
Apple.class_eval {        COLOR = "yellow"   }
#class Apple;   COLOR="yellow"  ; end
#Apple.const_set("COLOR", "green")
puts Apple::COLOR


运行结果:

引用
red
const_defined?  true
yellow



分享到:
评论

相关推荐

    redefine-function:重新定义功能

    重新定义功能 重新定义功能 安装 $ npm install --save redefine-function 用法 var redefineFunction = require ( "redefine-function" ) var Greetings = { hello : function ( ) { return "Hi there." } } ...

    redefine:用于智能对象属性定义的轻量级实用程序

    关于重新定义JS 这个约1.5KB(最小化并压缩)的框架在node.js和所有现代移动或桌面浏览器中带来了ES5和ES6功能的强大功能。 主要特点: ES6喜欢类定义,通过mixin和bound魔术具有额外的功能 没有描述符的ES5属性...

    redefine-properties:使用多个源对象定义属性

    通过使用提取的属性描述符重新定义属性,将多个对象的属性合并到目标中。 与多个来源一起使用。 复制所有自己的属性,包括无数属性。 对所有属性使用Object.defineProperty 。 如果发生冲突,则以财产的最后来源...

    backbone.redefine:骨干扩展,允许您重新定义类

    骨干重新定义安装要安装带有Bower的最新版本,请执行以下操作: > bower install backbone.redefine --save用法例如,请参阅jsdoc注释

    lambda 表达式导致 Arthas 无法 redefine 的问题

    主要介绍了lambda 表达式导致 Arthas 无法 redefine 的问题,本文通过图文实例相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

    CGH40_r6_converted.zip_CGH40- r6_CREE ADS_r6_cgh40_cgh40045 ads模

    ADS中的CREE管子的模型,自己编写的模型,可以正常使用

    Hexo之hexo-theme-redefine-main博客主题

    Hexo之hexo-theme-redefine-main博客主题

    DynamicNativeAgent:在没有工具或自定义类加载器的情况下重新定义Java类

    DynamicNativeAgent DynamicNativeAgent可以使用本机库重新定义Java类(无需类加载器或Instrumentation)用法AgentFactory . loadNative(); // Load the native in the JVMNativeAgent agent = AgentFactory . ...

    redefine方法

    作者danielalves,源码redefine,这是iOS和mac OS X一个简单的方法,在运行时的方法可以修正的。

    使用arthas命令redefine实现Java热更新(推荐)

    今天分享一个非常重要的命令 redefine ,主要作用是加载外部的 .class 文件,用来替换 JVM 已经加载的类,总结起来就是实现了 Java 的热更新,感兴趣的朋友跟随小编一起看看吧

    Effective C++(第三版)

    条款36:绝不重新定义继承而来的non-virtual函数 never redefine an inherited non-virtual function. 条款37:绝不重新定义继承而来的缺省参数值 never redefine a function's inherited default parameter value. ...

    agent-tools:一组用于将代理程序本地和远程地加载到JVM的工具的集合

    #agent-tools允许将本地和远程将代理加载到JVM的工具集合。... classNames)->使用当前加载的ctfs重新定义类 getObjectSize(Object obj)->返回对象的大小(以字节为单位) (远程版本)->与本地相

    ExpandClickArea:通过重新定义网格来扩展UGUI单击区域

    ExpandClickAreaExpand UGUI Click Area By Redefine Mesh分为三部分ExpandAreaImage 继承image,按比例扩大图片的矩形宽高,缩小图片的网格ExpandAreaImageEditor 编辑器下调整缩放比例参数UiToolEditor 提供编辑器...

    AT89C2051 RS232

    AT89C2051与上位机RS232通讯接口子程序

    An Introduction to Data

    An Introduction to Data: Everything You Need to Know About AI, Big Data and Data Science (Studies in Big Data) By 作者: Francesco Corea ISBN-10 书号: 303004467X ISBN-13 书号: 9783030044671 Edition ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX In some cases TFlexCurve.Paint leaves selected black pen in the canvas which then affects on next flex-control output (in particular when the next flex-control have gradient fill). FlexEdit...

    Android异步多线程加载图片

    // step one redefine your adapter which texteds ImageLoadAdapter or ImageLoadViewPageAdapter,here we named it TestImageLoadAdapter TestImageLoadAdapter adapter = new TestImageLoadAdapter(this); ...

    Multiline balloon tooltips on ListView items

    In the MouseMove event you need to check the index of the item under the mouse pointer, and if this item is changed, you simply redefine the text of the tooltip attached to the ListView control....

    Implementing OpenShift

    This book will show you how the OpenShift Platform-as-a-Service can redefine the way web application developers work by providing the building blocks upon which they are able to create their next big ...

    人脸识别MB-LBP

    In this paper, we propose a novel representation, calledMultiscale Block Local Binary Pattern (MB-LBP), and apply it to face recognition. The Local Binary Pattern (LBP) has been proved to be effective...

Global site tag (gtag.js) - Google Analytics