国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Ruby編程中的語法使用風(fēng)格推薦

2019-10-26 19:28:12
字體:
供稿:網(wǎng)友

使用 :: 引用常量(包括類和模塊)和構(gòu)造器 (比如 Array() 或者 Nokogiri::HTML())。
    永遠(yuǎn)不要使用 :: 來調(diào)用方法。

  # bad  SomeClass::some_method  some_object::some_method  # good  SomeClass.some_method  some_object.some_method  SomeModule::SomeClass::SOME_CONST  SomeModule::SomeClass()

    使用括號將def的參數(shù)括起來。當(dāng)方法不接收任何參數(shù)的時(shí)候忽略括號。

     

# bad   def some_method()    # body omitted   end   # good   def some_method    # body omitted   end   # bad   def some_method_with_arguments arg1, arg2    # body omitted   end   # good   def some_method_with_arguments(arg1, arg2)    # body omitted   end

    從來不要使用 for, 除非你知道使用它的準(zhǔn)確原因。大多數(shù)時(shí)候迭代器都可以用來替for。for 是由一組 each 實(shí)現(xiàn)的 (因此你正間接添加了一級),但是有一個小道道 - for并不包含一個新的 scope (不像 each)并且在它的塊中定義的變量在外面也是可以訪問的。

 

  arr = [1, 2, 3]  # bad  for elem in arr do   puts elem  end  # note that elem is accessible outside of the for loop  elem #=> 3  # good  arr.each { |elem| puts elem }  # elem is not accessible outside each's block  elem #=> NameError: undefined local variable or method `elem'

    在多行的 if/unless 中堅(jiān)決不要使用 then。

    

# bad  if some_condition then   # body omitted  end  # good  if some_condition   # body omitted  end

    在多行的 if/unless 總是把條件放在與 if/unless 的同一行。

 

  # bad  if   some_condition   do_something   do_something_else  end  # good  if some_condition   do_something   do_something_else  end

    喜歡三元操作運(yùn)算(?:)超過if/then/else/end結(jié)構(gòu)。
    它更加普遍而且明顯的更加簡潔。

  # bad  result = if some_condition then something else something_else end  # good  result = some_condition ? something : something_else

    使用一個表達(dá)式在三元操作運(yùn)算的每一個分支下面只使用一個表達(dá)式。也就是說三元操作符不要被嵌套。在這樣的情形中寧可使用 if/else。

  # bad  some_condition ? (nested_condition ? nested_something : nested_something_else) : something_else  # good  if some_condition   nested_condition ? nested_something : nested_something_else  else   something_else  end

    不要使用 if x: ... - 它在Ruby 1.9中已經(jīng)移除。使用三元操作運(yùn)算代替。

  # bad  result = if some_condition then something else something_else end  # good  result = some_condition ? something : something_else            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 徐水县| 双峰县| 大庆市| 安平县| 双柏县| 忻城县| 凉山| 兰西县| 南和县| 漯河市| 谢通门县| 广灵县| 曲麻莱县| 桓仁| 临桂县| 高州市| 句容市| 大同县| 济阳县| 南安市| 金沙县| 石家庄市| 桃园县| 绍兴市| 合江县| 攀枝花市| 西丰县| 文水县| 霸州市| 永安市| 东海县| 广灵县| 丁青县| 肃宁县| 曲水县| 鸡东县| 松江区| 汉寿县| 晋中市| 巩义市| 视频|