- 浏览: 233666 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (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 1976http://kidlet.sinaapp.com/blog/ ... -
ActiveRecord::Dirty
2011-11-21 10:29 737引用Track unsaved attribute chang ... -
Metaprogramming Ruby
2011-09-30 16:11 1102P30 In a sense, the class keywo ... -
RVM Install
2011-09-17 15:17 779http://beginrescueend.com/ -
json
2011-09-15 09:51 718http://flori.github.com/json/ -
Rails计算某月最后一天
2011-08-12 10:46 1373经常忘记这个函数.mark下. 引用end_of_day, e ... -
关于浮点数精度的问题
2011-05-11 15:50 1213在项目里遇到一个很诡异的问题,因为有一些浮点数的计算,总 ... -
Ruby Memoization(转载)
2010-11-28 23:45 785转载http://fuliang.iteye.com/blog ... -
included() vs extended()
2010-11-04 19:48 718# A little helper from _why cl ... -
ruby的to_proc
2010-10-21 00:41 8591,先看api 引用Method#proc meth.to_p ... -
Nesting Is Different From Inclusion
2010-10-17 10:02 743Nesting Is Different From Inclu ... -
Regular Expressions
2010-10-16 22:55 853... -
ruby里的方法作用域
2010-08-11 09:51 1042在java里private方法在Java当中的含义是只在当前类 ... -
Benchmark
2010-06-17 14:10 8101,length > 0和blank?和emtpy? & ... -
ruby的笔记
2010-05-20 14:23 838最近看了看ruby元编程的一些东西。简单的记下。 1,ruby ... -
闭包(回顾,转载)
2010-03-22 23:02 771闭包的一个重要特征是:过程(方法)内部定义的变量,即使在方法调 ... -
ruby cookbook -- 10.7检查对象是否具有必需的属性
2010-03-01 23:51 734检查是否具有实例变量 class Object de ... -
ruby cookbook -- 10.6. Listening for Changes to a Class监听类的变化
2010-03-01 23:30 732当增加新方法,类方法删除和取消定义的现有方法 class T ... -
ruby cookbook -- 10.4Getting a Reference to a Method(获得方法引用)
2010-03-01 23:25 788A Method object can be stored ... -
irb配置
2010-02-24 13:21 1028#.irbrc require 'rubygems' ...
相关推荐
PySimpleGUI cookbook-zh-CN 中文版 非机器翻译(目录标题未翻译)
Boost C++ Application Development Cookbook - Second Edition
coverage-cookbook-complete-verification-academy Candence Academy官方coverage文档
python-machine-learning-cookbook-preprocessing oreilly 英文 epub格式
Ruby Cookbook: Recipes for Object-Oriented Scripting Why spend time on coding problems that others have already solved when you could be making real progress on your Ruby project? This updated ...
Modern-CPP-Programming-Cookbook-Second-Edition-master Packt 出版 仅供学习参考,阅后删除。
an4776-generalpurpose-timer-cookbook-for-stm32-microcontrollers-stmicroelectro
The Network Design Cookbook explains not only the importance of designing an ideal network, but also the consequences if the design is not well thought out. This book will be a valuable tool in both ...
Programming ArcGIS with Python Cookbook - Second Edition, mobi格式
Programming ArcGIS with Python Cookbook - Second Edition,epub格式
nginx-cookbook-recipes-high-performance
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 # 访问 ...
uvm-cookbook-registers-guide-verification-academy
modern-cpp-programming-cookbook-2nd
KVM实操手册, 英语简练易懂, 讲的面比较全, KVM的相关内容基本都涉及到了, 内容全面,可操作性强.
uvm-cookbook-complete-verification-academy