a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Ruby > 正文

Ruby中的集合編寫指南

2020-10-29 19:37:33
字體:
來源:轉載
供稿:網友

傾向數(shù)組及哈希的字面表示法(除非你需要傳遞參數(shù)到它們的構造函數(shù)中)。

  # bad  arr = Array.new  hash = Hash.new  # good  arr = []  hash = {}

    當你需要元素為單詞(沒有空格和特殊符號)的數(shù)組的時候總是使用 %w 的方式來定義字符串數(shù)組。應用這條規(guī)則僅僅在兩個或多個數(shù)組。

  # bad  STATES = ['draft', 'open', 'closed']  # good  STATES = %w(draft open closed)

    當你需要一個符號的數(shù)組(并且不需要保持 Ruby 1.9 兼容性)時,使用 %i。僅當數(shù)組只有兩個及以上元素時才應用這個規(guī)則。

  # bad  STATES = [:draft, :open, :closed]  # good  STATES = %i(draft open closed)

    避免在 Array 或者 Hash 的最后一項后面出現(xiàn)逗號,特別是當這些條目不在一行。

  # 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]

    避免在數(shù)組中創(chuàng)造巨大的間隔。

  arr = []  arr[100] = 1 # now you have an array with lots of nils

    當訪問一個數(shù)組的第一個或者最后一個元素,傾向使用 first 或 last 而不是 [0] 或 [-1]。

    如果要確保元素唯一, 則使用 Set 代替 Array .Set 更適合于無順序的, 并且元素唯一的集合, 集合具有類似于數(shù)組一致性操作以及哈希的快速查找.

    盡可能使用符號代替字符串作為哈希鍵.

  # bad  hash = { 'one' => 1, 'two' => 2, 'three' => 3 }  # good  hash = { one: 1, two: 2, three: 3 }

    避免使用易變對象作為哈希鍵。

    優(yōu)先使用 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? 不用 Hash#has_key? 以及用 Hash#value?, 不用 Hash#has_value? Matz 提到過 長的形式在考慮被棄用。

  # bad  hash.has_key?(:test)  hash.has_value?(value)  # good  hash.key?(:test)  hash.value?(value)

    在處理應該存在的哈希鍵時,使用 fetch。

  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 時,使用第二個參數(shù)設置默認值而不是使用自定義的邏輯。

  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 加區(qū)塊而不是直接設定默認值。

  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 連續(xù)的取回一系列的值的時候使用 Hash#values_at。

  # bad  email = data['email']  nickname = data['nickname']  # good  email, username = data.values_at('email', 'nickname')

    記住, 在 Ruby1.9 中, 哈希的表現(xiàn)不再是無序的. (譯者注: Ruby1.9 將會記住元素插入的序列)

    當遍歷一個集合的同時, 不要修改這個集合。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美一区视频 | 懂色av中文一区二区三区天美 | 亚洲影视一区 | 精品综合| 91免费在线| 精品国产三级 | www.久草.com | 欧美另类一区二区 | 中国特黄毛片 | 日本视频在线 | 欧美一级片在线 | 一本大道综合伊人精品热热 | a在线天堂 | www在线观看国产 | 国产综合精品一区二区三区 | 久久韩剧网 | 精品欧美一区二区精品久久久 | 亚洲区一区二 | 国产精品精品 | 欧美成人性生活视频 | 欧美一二区 | 国产在线中文字幕 | 国产综合久久久久久鬼色 | 中文在线一区二区 | 自拍视频在线 | 久久专区| 成人黄色在线 | 国产精品成av人在线视午夜片 | 欧美精品一区二区三区在线播放 | 一区二区精品视频 | 国产亚洲成av人片在线观看桃 | 久久久久久毛片免费观看 | 蜜桃精品久久久久久久免费影院 | 成人免费观看cn | 亚洲毛片 | 久久久a | 一区二区三区在线 | 久久99精品久久久水蜜桃 | 爱爱免费视频网站 | 欧美一区二区三区在线看 | 亚洲国产精品成人综合色在线婷婷 |