- 浏览: 244771 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (173)
- ruby (38)
- rails (42)
- javascript (7)
- jquery (1)
- linux (15)
- design patterns (1)
- project management (6)
- IT (7)
- life (19)
- data structures and algorithm analysis (2)
- css (1)
- prototype (1)
- mysql (4)
- html (1)
- git (3)
- novels (1)
- c (1)
- Latex (13)
- erlang (1)
- 求职 (1)
- API (0)
- Shell (4)
- Rabbit MQ (1)
- 计算机基础 (1)
- svn (2)
- 疑问 (1)
最新评论
-
zhangyou1010:
回去倒立去,哈哈。
作为一个程序员,身体很重要! -
Hooopo:
Ruby MetaProgramming is all abo ...
Metaprogramming Ruby -
orcl_zhang:
yiqi1943 写道LZ现在上学还是工作呢工作好多年了。不过 ...
2011年 -
yiqi1943:
LZ现在上学还是工作呢
2011年 -
tjcjc:
query cache
就是一个简单的hash
key就是sq ...
Rails sql延迟加载和自带缓存
Recipe 8.16. Making a Copy of an Object
The downside of dup is that it creates a new instance of the object's original class. If you open up a specific object and give it a singleton method, you implicitly create a metaclass, an anonymous subclass of the original class. Calling dup on the object will yield a copy that lacks the singleton methods. The other object-copy method, Object#clone, makes a copy of the metaclass and instantiates the copy, instead of instantiating the object's original class.
Object#clone and Object#dup both perform shallow copies: they make copies of an object without also copying its instance variables. You'll end up with two objects whose instance variables point to the same objects. Modifications to one object's instance variables will be visible in the other object. This can cause problems if you're not expecting it:
If you want to do a deep copy, an easy (though not particularly quick) way is to serialize the object to a binary string with Marshal, then load a new object from the string:
Note that this will only work on an object that has no singleton methods:
When an object is cloned or duplicated, Ruby creates a new instance of its class or superclass, but without calling the initialize method. If you want to define some code to run when an object is cloned or duplicated, define an initialize_copy method. This is a hook method that gives you a chance to modify the copy before Ruby passes it back to whoever called clone or dup. If you want to simulate a deep copy without using Marshal, this is your chance to modify the copy's instance variables:
The downside of dup is that it creates a new instance of the object's original class. If you open up a specific object and give it a singleton method, you implicitly create a metaclass, an anonymous subclass of the original class. Calling dup on the object will yield a copy that lacks the singleton methods. The other object-copy method, Object#clone, makes a copy of the metaclass and instantiates the copy, instead of instantiating the object's original class.
material = 'cotton' class << material def definition puts 'The better half of velour.' end end material.definition # The better half of velour. 'cotton'.definition # NoMethodError: undefined method 'definition' for "cotton":String material.clone.definition # The better half of velour. material.dup.definition # NoMethodError: undefined method 'definition' for "cotton":String
Object#clone and Object#dup both perform shallow copies: they make copies of an object without also copying its instance variables. You'll end up with two objects whose instance variables point to the same objects. Modifications to one object's instance variables will be visible in the other object. This can cause problems if you're not expecting it:
class StringHolder attr_reader :string def initialize(string) @string = string end end s1 = StringHolder.new('string') s2 = s1.dup s3 = s1.clone s1.string[1] = 'p' s2.string # => "spring" s3.string # => "spring"
If you want to do a deep copy, an easy (though not particularly quick) way is to serialize the object to a binary string with Marshal, then load a new object from the string:
class Object def deep_copy Marshal.load(Marshal.dump(self)) end end s1 = StringHolder.new('string') s2 = s1.deep_copy s1.string[1] = 'p' s1.string # => "spring" s2.string # => "string"
Note that this will only work on an object that has no singleton methods:
class << s1 def definition puts "We hold strings so you don't have to." end end s1.deep_copy # TypeError: singleton can't be dumped
When an object is cloned or duplicated, Ruby creates a new instance of its class or superclass, but without calling the initialize method. If you want to define some code to run when an object is cloned or duplicated, define an initialize_copy method. This is a hook method that gives you a chance to modify the copy before Ruby passes it back to whoever called clone or dup. If you want to simulate a deep copy without using Marshal, this is your chance to modify the copy's instance variables:
class StringHolder def initialize_copy(from) @string = from.string.dup end end s1 = StringHolder.new('string') s2 = s1.dup s3 = s1.clone s1.string[1] = "p" s2.string # => "string" s3.string # => "string"
发表评论
-
Ruby 搭建环境
2013-06-01 11:17 2067http://kidlet.sinaapp.com/blog/ ... -
ActiveRecord::Dirty
2011-11-21 10:29 793引用Track unsaved attribute chang ... -
Metaprogramming Ruby
2011-09-30 16:11 1197P30 In a sense, the class keywo ... -
RVM Install
2011-09-17 15:17 910http://beginrescueend.com/ -
json
2011-09-15 09:51 844http://flori.github.com/json/ -
Rails计算某月最后一天
2011-08-12 10:46 1464经常忘记这个函数.mark下. 引用end_of_day, e ... -
关于浮点数精度的问题
2011-05-11 15:50 1291在项目里遇到一个很诡异的问题,因为有一些浮点数的计算,总 ... -
Ruby Memoization(转载)
2010-11-28 23:45 829转载http://fuliang.iteye.com/blog ... -
included() vs extended()
2010-11-04 19:48 767# A little helper from _why cl ... -
ruby的to_proc
2010-10-21 00:41 9181,先看api 引用Method#proc meth.to_p ... -
Nesting Is Different From Inclusion
2010-10-17 10:02 803Nesting Is Different From Inclu ... -
Regular Expressions
2010-10-16 22:55 910... -
ruby里的方法作用域
2010-08-11 09:51 1106在java里private方法在Java当中的含义是只在当前类 ... -
Benchmark
2010-06-17 14:10 9101,length > 0和blank?和emtpy? & ... -
ruby的笔记
2010-05-20 14:23 981最近看了看ruby元编程的一些东西。简单的记下。 1,ruby ... -
闭包(回顾,转载)
2010-03-22 23:02 865闭包的一个重要特征是:过程(方法)内部定义的变量,即使在方法调 ... -
ruby cookbook -- 10.7检查对象是否具有必需的属性
2010-03-01 23:51 791检查是否具有实例变量 class Object de ... -
ruby cookbook -- 10.6. Listening for Changes to a Class监听类的变化
2010-03-01 23:30 791当增加新方法,类方法删除和取消定义的现有方法 class T ... -
ruby cookbook -- 10.4Getting a Reference to a Method(获得方法引用)
2010-03-01 23:25 834A Method object can be stored ... -
irb配置
2010-02-24 13:21 1091#.irbrc require 'rubygems' ...
相关推荐
"cookbook-zh-CN.md"文件很可能是这本书的主要文本部分,里面详细列举了各种使用PySimpleGUI的技巧和实践案例。每个章节通常会介绍一个特定的功能或概念,并配有相应的代码示例。通过阅读和实践这些例子,开发者可以...
python-machine-learning-cookbook-preprocessing oreilly 英文 epub格式
标题“coverage-cookbook-complete-verification-academy”表明这是一本关于覆盖度(coverage)的食谱手册,隶属于Cadence Academy的官方文件。这种手册通常包含一系列经过精心设计的指导方案,旨在帮助读者理解和...
《CMake Cookbook》是关于构建、测试和打包模块化软件的专业指南,专注于现代CMake工具的使用。本书由Radovan Bast和Roberto Di Remigio合著,旨在帮助读者掌握CMake这一强大的跨平台构建系统。 CMake是一个开源的...
Programming ArcGIS with Python Cookbook - Second Edition, mobi格式
Programming ArcGIS with Python Cookbook - Second Edition,epub格式
If you are a beginner or an intermediate Linux user who wants to master the skill of quickly writing scripts and automate tasks without reading the entire man pages, then this book is for you....
Unity Game Development Cookbook - Paris Buttfield-AddisonUnity Game Development Cookbook - Paris Buttfield-Addison
docker run -tid -p <port>:80 apachecn0/pandas-cookbook-code-notes # 访问 http://localhost:{port} 查看文档 PYPI pip install pandas-cookbook-code-notes pandas-cookbook-code-notes # 访问 ...
Lott -- Modern Python Cookbook -- 2016 -- code.7z
Aggarwal -- Flask Framework Cookbook -- 2014 -- code.7z
"NGINX Cookbook 高性能负载平衡高级配方" NGINX Cookbook 是一本专门介绍 NGINX 高性能负载平衡的书籍,书中涵盖了 NGINX 的高级配方和使用技巧,可以帮助读者快速搭建高性能的负载平衡系统。 GeoIP 模块和数据库...
Subramanian -- Python Data Science Cookbook -- 2015 -- code.7z
Fine -- Python 2.6 Graphics Cookbook -- 2010 -- code.7z
Zaccone -- Python Parallel Programming Cookbook -- 2015 -- code.7z