校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > Ruby中的集合編寫(xiě)指南

Ruby中的集合編寫(xiě)指南

熱門(mén)標(biāo)簽:滴滴外呼系統(tǒng) 地圖標(biāo)注賺錢(qián)項(xiàng)目注冊(cè) 常德電銷(xiāo)平臺(tái)外呼系統(tǒng)軟件價(jià)格 百度地圖標(biāo)注自定義圖片 徐州網(wǎng)絡(luò)外呼系統(tǒng)哪個(gè)好 湖州u友防封電銷(xiāo)卡 高德地圖標(biāo)注客服 白銀外呼paas系統(tǒng) 電銷(xiāo)機(jī)器人廠(chǎng)商代理

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

  # bad
  arr = Array.new
  hash = Hash.new

  # good
  arr = []
  hash = {}

    當(dāng)你需要元素為單詞(沒(méi)有空格和特殊符號(hào))的數(shù)組的時(shí)候總是使用 %w 的方式來(lái)定義字符串?dāng)?shù)組。應(yīng)用這條規(guī)則僅僅在兩個(gè)或多個(gè)數(shù)組。

  # bad
  STATES = ['draft', 'open', 'closed']

  # good
  STATES = %w(draft open closed)

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

  # bad
  STATES = [:draft, :open, :closed]

  # good
  STATES = %i(draft open closed)

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

  # 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

    當(dāng)訪(fǎng)問(wèn)一個(gè)數(shù)組的第一個(gè)或者最后一個(gè)元素,傾向使用 first 或 last 而不是 [0] 或 [-1]。

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

    盡可能使用符號(hào)代替字符串作為哈希鍵.

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

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

    避免使用易變對(duì)象作為哈希鍵。

    優(yōu)先使用 1.9 的新哈希語(yǔ)法當(dāng)你的哈希鍵是符號(hào)。

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

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

    在相同的 hash 字面量中不要混合 Ruby 1.9 hash 語(yǔ)法和箭頭形式的 hash。當(dāng)你
    得到的 keys 不是符號(hào)的時(shí)候轉(zhuǎn)換為箭頭形式的語(yǔ)法。

  # bad
  { a: 1, 'b' => 2 }

  # good
  { :a => 1, 'b' => 2 }

    用 Hash#key? 不用 Hash#has_key? 以及用 Hash#value?, 不用 Hash#has_value? Matz 提到過(guò) 長(zhǎng)的形式在考慮被棄用。

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

  # good
  hash.key?(:test)
  hash.value?(value)

    在處理應(yīng)該存在的哈希鍵時(shí),使用 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í),使用第二個(gè)參數(shù)設(shè)置默認(rèn)值而不是使用自定義的邏輯。

  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ū)塊而不是直接設(shè)定默認(rèn)值。

  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 }

    當(dāng)你需要從一個(gè) hash 連續(xù)的取回一系列的值的時(shí)候使用 Hash#values_at。

  # bad
  email = data['email']
  nickname = data['nickname']

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

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

    當(dāng)遍歷一個(gè)集合的同時(shí), 不要修改這個(gè)集合。

標(biāo)簽:遼寧 普洱 三沙 公主嶺 梧州 永州 荊門(mén) 張家界

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby中的集合編寫(xiě)指南》,本文關(guān)鍵詞  Ruby,中的,集合,編寫(xiě),指南,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ruby中的集合編寫(xiě)指南》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Ruby中的集合編寫(xiě)指南的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 湖南省| 施秉县| 边坝县| 会宁县| 都昌县| 恩施市| 仪陇县| 鄂托克旗| 乌鲁木齐市| 社会| 鹰潭市| 四川省| 阿尔山市| 抚顺市| 年辖:市辖区| 自治县| 新巴尔虎左旗| 德州市| 新乐市| 松潘县| 万源市| 平山县| 南皮县| 孟津县| 和田市| 鄯善县| 光泽县| 龙州县| 蒙自县| 庄河市| 德江县| 石景山区| 左权县| 武威市| 沙雅县| 呼伦贝尔市| 高雄县| 尼勒克县| 石泉县| 乌拉特中旗| 吉安市|