【翻译】在ruby中实现attributes的lazily initialization(惰性初始化)
【翻译】Ruby: Lazily Initialized Attributes
url(注:原文网址访问不到,估计是被和谐了):
http://blog.jayfields.com/2007/07/ruby-lazily-initialized-attributes.html
http://95555.info/index.php?q=aHR0cDovL2Jsb2cuamF5ZmllbGRzLmNvbS8yMDA3LzA3L3J1YnktbGF6aWx5LWluaXRpYWxpemVkLWF0dHJpYnV0ZXMuaHRtbA%3D%3D&hl=0111101001&s=mza5mdcxmjewndjon01vbjbwbq==
在第一次访问一个attribute时对其进行初始化,而不是在构造函数里面完成。
[code]
class Employee
def initialize
@emails = []
end
end
[/code]
变为
[code]
class Employee
def emails
unless instance_variable_defined?(:@emails)
@emails = []
end
@emails
end
end
[/code]
动机:
增加代码可读性。
上述例子非常简单。如果Employee这个类有多个属性需要初始化的话,构造函数就需要包含所有的初始化逻辑。而采取lazy initialization的方式可以把各个属性的初始化逻辑放置在它们自己的访问函数中。
运作机制:
将初始化逻辑移到属性的getter方法中
示例见上。
转载于:https://www.cnblogs.com/bryanzk/archive/2007/07/30/836501.html
【翻译】在ruby中实现attributes的lazily initialization(惰性初始化)相关推荐
- Ruby中的require_relative和require有什么区别?
本文翻译自:What is the difference between require_relative and require in Ruby? Ruby中的require_relative和re ...
- 在Ruby中获取system()调用的输出
本文翻译自:Getting output of system() calls in Ruby 如果我在Ruby中使用Kernel#system调用命令,我该如何获得它的输出? system(" ...
- 为什么在Ruby中使用`rescue Exception =gt; e`样式不好?
本文翻译自:Why is it bad style to `rescue Exception => e` in Ruby? Ryan Davis's Ruby QuickRef says (wi ...
- ruby 集合 分组_在Ruby中打印集合的元素
ruby 集合 分组 We have gone through the implementation of sets in Ruby. They are very similar to arrays. ...
- ruby中、.reject_Ruby中带有示例的Array.reject方法
ruby中..reject Ruby Array.reject方法 (Ruby Array.reject Method) In the last article, we have seen how w ...
- ruby 集合 分组_在Ruby中找到两个集合之间的区别
ruby 集合 分组 Finding differences simply means that finding elements that are uncommon between two sets ...
- ruby 集合 分组_将Ruby中两个集合的所有元素结合在一起
ruby 集合 分组 In this program, we will see how we can combine the two sets? This is not a very difficul ...
- ruby array_在Ruby中使用Array.pop和Array.shift方法从Array中删除元素
ruby array Ruby Array.pop和Array.shift方法 (Ruby Array.pop and Array.shift methods) If you are reading ...
- ruby中!!_Ruby反向! 功能
ruby中!! 逆转! Ruby中的功能 (reverse! function in Ruby) As the name suggests, reverse! function is used to ...
最新文章
- 一分钟详解PCL中点云配准技术
- hdu 2553 N皇后问题(深度递归搜索)
- 《JavaScript高级程序设计》读书笔记 ---基本概念小结
- 【Linux】一步一步学Linux——Linux内核版本和发行版本(03)
- 超低内阻mos管_FHU100N03低压MOS管保障榨汁机电路正常运行!
- 我从创建具有仅仅一年编码经验的视频游戏中学到了什么
- php递归算法排序,php全排列递归算法代码
- java语言的科学与艺术-编程练习10.4
- sql server 群集_SQL Server群集索引概述
- Bailian2737 大整数除法【大数】
- 显示桌面 图标 创建可以新建一个txt文件,把以下内容放入,名字改为显示桌面 .scf...
- 第一类丢失更新和第二类丢失更新是什么 区别
- mount -t iso9660 /dev/cdrom /mnt
- abb变频器580系列改中文_ABB变频器ACS580系列
- PPAPI插件开发指南
- 对booth算法的一点理解
- bt4 linux 镜像,让你的启动U盘集合7个系统包括pe bt4 beibi q3 linux dos等
- linux解压时,z x v f分别代表什么意思
- 【Python123】鸡兔同笼
- 推荐12个免费查找文献的学术网站,建议点赞、收藏!
热门文章
- Solaris 的防火墙ipfilter设置
- Django实现一个简单的中间件,不熟悉中间件的爬坑之路
- 基于redis(v3.2+)实现“附近的人”功能
- linux安装两个jdk_jdk在linux上安装过程
- mfc读取bmp文件_LIUNX 工程bmp.c 显示图片 开发板出来的图片只有一个角 求大佬解决...
- 彻底理解被称为二叉树神级遍历的莫里斯(Morris)算法
- 如何对CAD进行区域覆盖
- 隐藏方法不能实现多态性
- React+Webpack+Eslint+Babel构建React脚手架
- 【HTTP】POST 与 PUT 方法区别