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

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

淺析Ruby的源代碼布局及其編程風(fēng)格

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

使用 UTF-8 作為源文件編碼。

    每個(gè)縮進(jìn)級(jí)別使用兩個(gè) spaces (又名軟 tabs). 不要硬 tabs

  # bad - four spaces  def some_method    do_something  end  # good  def some_method   do_something  end

    使用 Unix-風(fēng)格 換行符。(*BSD/Solaris/Linux/OSX 用戶被為默認(rèn)涵蓋,Windows 用戶必須特別小心.)

        /n是換行,英文是LineFeed,ASCII碼是0xA。         /r是回車,英文是Carriage Return ,ASCII碼是0xD。         windows下enter是 /n/r,unix下是/n,mac下是/r

        如果你正在使用 Git 你可能會(huì)想要添加下面的配置設(shè)置來保護(hù)你的項(xiàng)目(避免)Windows 蔓延過來的換行符:

 

  $ git config --global core.autocrlf true

    不用使用 ; 來分割語句和表達(dá)式。以此推論 - 一行使用一個(gè)表達(dá)式

  

 # bad  puts 'foobar'; # superfluous semicolon  puts 'foo'; puts 'bar' # two expression on the same line  # good  puts 'foobar'  puts 'foo'  puts 'bar'  puts 'foo', 'bar' # this applies to puts in particular

    對于沒有內(nèi)容的類定義,盡可能使用單行類定義形式.

   

 # bad  class FooError < StandardError  end  # okish  class FooError < StandardError; end  # good  FooError = Class.new(StandardError)

    避免單行方法。即便還是會(huì)受到一些人的歡迎,這里還是會(huì)有一些古怪的語法用起來很容易犯錯(cuò).
    無論如何 - 應(yīng)該一行不超過一個(gè)單行方法.

    

# bad  def too_much; something; something_else; end  # okish - notice that the first ; is required  def no_braces_method; body end  # okish - notice that the second ; is optional  def no_braces_method; body; end  # okish - valid syntax, but no ; make it kind of hard to read  def some_method() body end  # good  def some_method   body  end

    空方法是這個(gè)規(guī)則的例外。

  # good  def no_op; end

    操作符旁的空格,在逗號(hào),冒號(hào)和分號(hào)后;在 { 旁和在 } 之前,大多數(shù)空格可能對 Ruby 解釋(代碼)無關(guān),但是它的恰當(dāng)使用是讓代碼變得易讀的關(guān)鍵。

  sum = 1 + 2  a, b = 1, 2  1 > 2 ? true : false; puts 'Hi'  [1, 2, 3].each { |e| puts e }

    唯一的例外是當(dāng)使用指數(shù)操作時(shí):

  # bad  e = M * c ** 2  # good  e = M * c**2

    { 和 } 值得額外的澄清,自從它們被用于 塊 和 hash 字面量,以及以表達(dá)式的形式嵌入字符串。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 普洱| 周口市| 旬阳县| 隆尧县| 读书| 长沙县| 包头市| 东乌珠穆沁旗| 视频| 新兴县| 涟水县| 盱眙县| 岳普湖县| 萨嘎县| 贡山| 庄河市| 井陉县| 海盐县| 惠州市| 清徐县| 柘荣县| 略阳县| 湾仔区| 武山县| 遵化市| 凤山县| 武城县| 乌兰浩特市| 喜德县| 金湖县| 久治县| 濮阳市| 昌宁县| 宿迁市| 凤凰县| 裕民县| 临高县| 公安县| 诏安县| 巴林左旗| 淮滨县|