随笔-167  评论-65  文章-0  trackbacks-0
ruby能够使用merge(别名update)方法,来合并两个hash,如果他们中的健有相同的话,一个将会被删除

demo:
delaware = { 1 => "Two", 2 => "New Value", 3 => "New Three" , 8 => "Five" }

myhash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five" }

puts delaware

puts myhash

puts delaware.merge(myhash)

puts myhash.merge(delaware)

结果:
1Two2New Value8Five3New Three
5Five1One2Two3Three4Four
5Five1One2Two8Five3Three4Four
5Five1Two2New Value8Five3New Three4Four

ref:
http://ruby-doc.org/core/classes/Hash.html#M002880
http://www.java2s.com/Code/Ruby/Hash/Mergetwohashes.htm
http://simohayha.javaeye.com/blog/151947







write by feng
posted on 2009-07-28 16:11 fl1429 阅读(2257) 评论(0)  编辑  收藏 所属分类: Rails

只有注册用户登录后才能发表评论。


网站导航:
 
已访问数:
free counters