- 浏览: 246504 次
- 性别:
- 来自: 杭州
-
文章分类
- 全部博客 (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延迟加载和自带缓存
Hash
1. Except for strings and other built-in objects, most objects have a hash code equivalent to their internal object ID. As seen above, you can override Object#hash to change this, but the only time you should need to do this is if your class also overrides Object#==. If two objects are considered equal, they should also have the same hash code; otherwise, they will behave strangely when you put them into hashes. Code like the fragment below is a very good idea:
Except for strings and other built-in objects, most objects have a hash code equivalent to their internal object ID. As seen above, you can override Object#hash to change this, but the only time you should need to do this is if your class also overrides Object#==. If two objects are considered equal, they should also have the same hash code; otherwise, they will behave strangely when you put them into hashes. Code like the fragment below is a very good idea:
Using Symbols as Hash Keys
While 'name' and 'name' appear exactly identical, they're actually different. Each time you create a quoted string in Ruby, you create a unique object. You can see this by looking at the object_id method.
By comparison, each instance of a symbol refers to a single object.
Using symbols instead of strings saves memory and time. It saves memory because there's only one symbol instance, instead of many string instances. If you have many hashes that contain the same keys, the memory savings adds up.
Using symbols as hash keys is faster because the hash value of a symbol is simply its object ID. If you use strings in a hash, Ruby must calculate the hash value of a string each time it's used as a hash key.
1. Except for strings and other built-in objects, most objects have a hash code equivalent to their internal object ID. As seen above, you can override Object#hash to change this, but the only time you should need to do this is if your class also overrides Object#==. If two objects are considered equal, they should also have the same hash code; otherwise, they will behave strangely when you put them into hashes. Code like the fragment below is a very good idea:
class StringHolder attr_reader :string def initialize(s) @string = s end def ==(other) @string == other.string end def hash @string.hash end end a = StringHolder.new("The same string.") b = StringHolder.new("The same string.") a == b # => true a.hash # => -1007666862 b.hash # => -1007666862
Except for strings and other built-in objects, most objects have a hash code equivalent to their internal object ID. As seen above, you can override Object#hash to change this, but the only time you should need to do this is if your class also overrides Object#==. If two objects are considered equal, they should also have the same hash code; otherwise, they will behave strangely when you put them into hashes. Code like the fragment below is a very good idea:
class StringHolder attr_reader :string def initialize(s) @string = s end def ==(other) @string == other.string end def hash @string.hash end end a = StringHolder.new("The same string.") b = StringHolder.new("The same string.") a == b # => true a.hash # => -1007666862 b.hash # => -1007666862
Using Symbols as Hash Keys
While 'name' and 'name' appear exactly identical, they're actually different. Each time you create a quoted string in Ruby, you create a unique object. You can see this by looking at the object_id method.
'name'.object_id # => -605973716 'name'.object_id # => -605976356 'name'.object_id # => -605978996
By comparison, each instance of a symbol refers to a single object.
:name.object_id # => 878862 :name.object_id # => 878862 'name'.intern.object_id # => 878862 'name'.intern.object_id # => 878862
Using symbols instead of strings saves memory and time. It saves memory because there's only one symbol instance, instead of many string instances. If you have many hashes that contain the same keys, the memory savings adds up.
Using symbols as hash keys is faster because the hash value of a symbol is simply its object ID. If you use strings in a hash, Ruby must calculate the hash value of a string each time it's used as a hash key.
发表评论
-
Ruby 搭建环境
2013-06-01 11:17 2077http://kidlet.sinaapp.com/blog/ ... -
ActiveRecord::Dirty
2011-11-21 10:29 797引用Track unsaved attribute chang ... -
Metaprogramming Ruby
2011-09-30 16:11 1201P30 In a sense, the class keywo ... -
RVM Install
2011-09-17 15:17 922http://beginrescueend.com/ -
json
2011-09-15 09:51 852http://flori.github.com/json/ -
Rails计算某月最后一天
2011-08-12 10:46 1479经常忘记这个函数.mark下. 引用end_of_day, e ... -
关于浮点数精度的问题
2011-05-11 15:50 1298在项目里遇到一个很诡异的问题,因为有一些浮点数的计算,总 ... -
Ruby Memoization(转载)
2010-11-28 23:45 832转载http://fuliang.iteye.com/blog ... -
included() vs extended()
2010-11-04 19:48 775# A little helper from _why cl ... -
ruby的to_proc
2010-10-21 00:41 9271,先看api 引用Method#proc meth.to_p ... -
Nesting Is Different From Inclusion
2010-10-17 10:02 812Nesting Is Different From Inclu ... -
Regular Expressions
2010-10-16 22:55 918... -
ruby里的方法作用域
2010-08-11 09:51 1112在java里private方法在Java当中的含义是只在当前类 ... -
Benchmark
2010-06-17 14:10 9351,length > 0和blank?和emtpy? & ... -
ruby的笔记
2010-05-20 14:23 990最近看了看ruby元编程的一些东西。简单的记下。 1,ruby ... -
闭包(回顾,转载)
2010-03-22 23:02 886闭包的一个重要特征是:过程(方法)内部定义的变量,即使在方法调 ... -
ruby cookbook -- 10.7检查对象是否具有必需的属性
2010-03-01 23:51 796检查是否具有实例变量 class Object de ... -
ruby cookbook -- 10.6. Listening for Changes to a Class监听类的变化
2010-03-01 23:30 794当增加新方法,类方法删除和取消定义的现有方法 class T ... -
ruby cookbook -- 10.4Getting a Reference to a Method(获得方法引用)
2010-03-01 23:25 844A Method object can be stored ... -
irb配置
2010-02-24 13:21 1097#.irbrc require 'rubygems' ...
相关推荐
"cookbook-zh-CN.md"文件很可能是这本书的主要文本部分,里面详细列举了各种使用PySimpleGUI的技巧和实践案例。每个章节通常会介绍一个特定的功能或概念,并配有相应的代码示例。通过阅读和实践这些例子,开发者可以...
Odoo 14 Development Cookbook - Fourth Edition(ebook-eglish) 代码
python-machine-learning-cookbook-preprocessing oreilly 英文 epub格式
标题“coverage-cookbook-complete-verification-academy”表明这是一本关于覆盖度(coverage)的食谱手册,隶属于Cadence Academy的官方文件。这种手册通常包含一系列经过精心设计的指导方案,旨在帮助读者理解和...
Programming ArcGIS with Python Cookbook - Second Edition, mobi格式
Programming ArcGIS with Python Cookbook - Second Edition,epub格式
《CMake Cookbook》是关于构建、测试和打包模块化软件的专业指南,专注于现代CMake工具的使用。本书由Radovan Bast和Roberto Di Remigio合著,旨在帮助读者掌握CMake这一强大的跨平台构建系统。 CMake是一个开源的...
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 # 访问 ...
Unity Game Development Cookbook - Paris Buttfield-AddisonUnity Game Development Cookbook - Paris Buttfield-Addison
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
Precord -- wxPython. Application Development Cookbook -- 2015 -- code.7z