倾向数组及哈希的字面表示法(除非你需要传递参数到它们的构造函数中)。
# bad arr = Array.new hash = Hash.new # good arr = [] hash = {}
当你需要元素为单词(没有空格和特殊符号)的数组的时候总是使用 %w 的方式来定义字符串数组。应用这条规则仅仅在两个或多个数组。
# bad STATES = ['draft', 'open', 'closed'] # good STATES = %w(draft open closed)
当你需要一个符号的数组(并且不需要保持 Ruby 1.9 兼容性)时,使用 %i。仅当数组只有两个及以上元素时才应用这个规则。
# bad STATES = [:draft, :open, :closed] # good STATES = %i(draft open closed)
避免在 Array 或者 Hash 的最后一项后面出现逗号,特别是当这些条目不在一行。
# bad - easier to move/add/remove items, but still not preferred VALUES = [ 1001, 2020, 3333, ] # bad VALUES = [1001, 2020, 3333, ] # good VALUES = [1001, 2020, 3333]
避免在数组中创造巨大的间隔。
arr = [] arr[100] = 1 # now you have an array with lots of nils
当访问一个数组的第一个或者最后一个元素,倾向使用 first 或 last 而不是 [0] 或 [-1]。
如果要确保元素唯一, 则使用 Set 代替 Array .Set 更适合于无顺序的, 并且元素唯一的集合, 集合具有类似于数组一致性操作以及哈希的快速查找.
尽可能使用符号代替字符串作为哈希键.
# bad hash = { 'one' => 1, 'two' => 2, 'three' => 3 } # good hash = { one: 1, two: 2, three: 3 }
避免使用易变对象作为哈希键。
优先使用 1.9 的新哈希语法当你的哈希键是符号。
# bad hash = { :one => 1, :two => 2, :three => 3 } # good hash = { one: 1, two: 2, three: 3 }
在相同的 hash 字面量中不要混合 Ruby 1.9 hash 语法和箭头形式的 hash。当你
得到的 keys 不是符号的时候转换为箭头形式的语法。
# bad { a: 1, 'b' => 2 } # good { :a => 1, 'b' => 2 }
用 Hash#key"htmlcode">
# bad hash.has_key"htmlcode">heroes = { batman: 'Bruce Wayne', superman: 'Clark Kent' } # bad - if we make a mistake we might not spot it right away heroes[:batman] # => "Bruce Wayne" heroes[:supermann] # => nil # good - fetch raises a KeyError making the problem obvious heroes.fetch(:supermann)在使用 fetch 时,使用第二个参数设置默认值而不是使用自定义的逻辑。
batman = { name: 'Bruce Wayne', is_evil: false } # bad - if we just use || operator with falsy value we won't get the expected result batman[:is_evil] || true # => true # good - fetch work correctly with falsy values batman.fetch(:is_evil, true) # => false尽量用 fetch 加区块而不是直接设定默认值。
batman = { name: 'Bruce Wayne' } # bad - if we use the default value, we eager evaluate it # so it can slow the program down if done multiple times batman.fetch(:powers, get_batman_powers) # get_batman_powers is an expensive call # good - blocks are lazy evaluated, so only triggered in case of KeyError exception batman.fetch(:powers) { get_batman_powers }当你需要从一个 hash 连续的取回一系列的值的时候使用 Hash#values_at。
# bad email = data['email'] nickname = data['nickname'] # good email, username = data.values_at('email', 'nickname')记住, 在 Ruby1.9 中, 哈希的表现不再是无序的. (译者注: Ruby1.9 将会记住元素插入的序列)
当遍历一个集合的同时, 不要修改这个集合。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 《阿瑞亚之旅》评测:更适合佛系玩家体质的跑团
- 黑神话悟空浮屠界紫门怎么开 浮屠界紫门打开方法介绍
- 黑神话悟空龟泪怎么获得 黑神话悟空龟泪获得方法介绍
- 黑神话悟空怎么获得还魂丹 黑神话悟空还魂丹获取方法一览
- HiFi顶级人声天碟《试音齐秦》wav
- 张敬轩.2012-WhyNot(EP)【环球】【WAV+CUE】
- 张小英1988-情愁[新加坡复黑版][WAV+CUE]
- 游民金币礼品补货啦 Steam充值卡等你兑换!
- 《暗喻幻想:ReFantazio》M站均分94分 近年最好的RPG游戏之一
- 今年第一项诺贝尔奖揭幕 生理学或医学奖揭晓
- 《梁静茹 华语乐坛情歌天后 爱久见人心》[WAV/分轨][360MB]
- 《田震 绝版珍藏 依然在飞》[WAV+CUE][600MB]
- 《60首经典金曲 4CD》[WAV/分轨][1.4GB]
- 赵传.1988-我很丑,可是我很温柔【滚石】【WAV+CUE】
- 遗忘曲线.2024-遗忘曲线(EP)【LuuvLabel】【FLAC分轨】