最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ruby中編寫類與模塊的風(fēng)格指南

 更新時間:2015年08月03日 12:08:38   投稿:goldensun  
這篇文章主要介紹了Ruby中編寫類與模塊的風(fēng)格指南,示例采用的也是Ruby編程中一些常用的編寫慣例,需要的朋友可以參考下

在 class 定義里使用一致的結(jié)構(gòu)。

    

class Person
   # extend and include go first
   extend SomeModule
   include AnotherModule

   # constants are next
   SOME_CONSTANT = 20

   # afterwards we have attribute macros
   attr_reader :name

   # followed by other macros (if any)
   validates :name

   # public class methods are next in line
   def self.some_method
   end

   # followed by public instance methods
   def some_method
   end

   # protected and private methods are grouped near the end
   protected

   def some_protected_method
   end

   private

   def some_private_method
   end
  end

    傾向使用 module,而不是只有類方法的 class。類別應(yīng)該只在創(chuàng)建實例是合理的時候使用。

   

 # bad
  class SomeClass
   def self.some_method
    # body omitted
   end

   def self.some_other_method
   end
  end

  # good
  module SomeClass
   module_function

   def some_method
    # body omitted
   end

   def some_other_method
   end
  end

    當(dāng)你希望將模塊的實例方法變成 class 方法時,偏愛使用 module_function 勝過 extend self。

 

  # bad
  module Utilities
   extend self

   def parse_something(string)
    # do stuff here
   end

   def other_utility_method(number, string)
    # do some more stuff
   end
  end

  # good
  module Utilities
   module_function

   def parse_something(string)
    # do stuff here
   end

   def other_utility_method(number, string)
    # do some more stuff
   end
  end

    When designing class hierarchies make sure that they conform to the
    Liskov Substitution Principle.

    在設(shè)計類層次的時候確保他們符合 Liskov Substitution Principle 原則。(譯者注: LSP原則大概含義為: 如果一個函數(shù)中引用了 父類的實例, 則一定可以使用其子類的實例替代, 并且函數(shù)的基本功能不變. (雖然功能允許被擴(kuò)展))

        Liskov替換原則:子類型必須能夠替換它們的基類型 <br/>
        1. 如果每一個類型為T1的對象o1,都有類型為T2的對象o2,使得以T1定義的所有程序P在所有的對象o1都代換為o2時,程序P的行為沒有變化,那么類型T2是類型T1的子類型。 <br/>
        2. 換言之,一個軟件實體如果使用的是一個基類的話,那么一定適用于其子類,而且它根本不能察覺出基類對象和子類對象的區(qū)別。只有衍生類替換基類的同時軟件實體的功能沒有發(fā)生變化,基類才能真正被復(fù)用。 <br/>
        3. 里氏代換原則由Barbar Liskov(芭芭拉.里氏)提出,是繼承復(fù)用的基石。 <br/>
        4. 一個繼承是否符合里氏代換原則,可以判斷該繼承是否合理(是否隱藏有缺陷)。

    努力使你的類盡可能的健壯 [SOLID](http://en.wikipedia.org/wiki/SOLID_object-oriented_design\))。(

    總是為你自己的類提供 to_s 方法, 用來表現(xiàn)這個類(實例)對象包含的對象.

   

 class Person
   attr_reader :first_name, :last_name

   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end

   def to_s
    "#@first_name #@last_name"
   end
  end

    使用 attr 功能成員來定義各個實例變量的訪問器或者修改器方法。

  

 # bad
  class Person
   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end

   def first_name
    @first_name
   end

   def last_name
    @last_name
   end
  end

  # good
  class Person
   attr_reader :first_name, :last_name

   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end
  end

    避免使用 attr。使用 attr_reader 和 attr_accessor 作為替代。

  # bad - creates a single attribute accessor (deprecated in 1.9)
  attr :something, true
  attr :one, :two, :three # behaves as attr_reader

  # good
  attr_accessor :something
  attr_reader :one, :two, :three

    考慮使用 Struct.new, 它可以定義一些瑣碎的 accessors,
    constructor(構(gòu)造函數(shù)) 和 comparison(比較) 操作。

  # good
  class Person
   attr_reader :first_name, :last_name

   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end
  end

  # better
  class Person < Struct.new(:first_name, :last_name)
  end

    考慮使用 Struct.new,它替你定義了那些瑣碎的存取器(accessors),構(gòu)造器(constructor)以及比較操作符(comparison operators)。

  # good
  class Person
   attr_accessor :first_name, :last_name

   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end
  end

  # better
  Person = Struct.new(:first_name, :last_name) do
  end

    不要去 extend 一個 Struct.new - 它已經(jīng)是一個新的 class。擴(kuò)展它會產(chǎn)生一個多余的 class 層級
    并且可能會產(chǎn)生怪異的錯誤如果文件被加載多次。

    考慮添加工廠方法來提供靈活的方法來創(chuàng)建特定類實例。

    

class Person
   def self.create(potions_hash)
    # body omitted
   end
  end

    鴨子類型(duck-typing)優(yōu)于繼承。

  

 # bad
  class Animal
   # abstract method
   def speak
   end
  end

  # extend superclass
  class Duck < Animal
   def speak
    puts 'Quack! Quack'
   end
  end

  # extend superclass
  class Dog < Animal
   def speak
    puts 'Bau! Bau!'
   end
  end

  # good
  class Duck
   def speak
    puts 'Quack! Quack'
   end
  end

  class Dog
   def speak
    puts 'Bau! Bau!'
   end
  end

    Avoid the usage of class (@@) variables due to their "nasty" behavior
    in inheritance.

    避免使用類變量(@@)因為他們討厭的繼承習(xí)慣(在子類中也可以修改父類的類變量)。

   

 class Parent
   @@class_var = 'parent'

   def self.print_class_var
    puts @@class_var
   end
  end

  class Child < Parent
   @@class_var = 'child'
  end

  Parent.print_class_var # => will print "child"

    正如上例看到的, 所有的子類共享類變量, 并且可以直接修改類變量,此時使用類實例變量是更好的主意.

    根據(jù)方法的用途為他們分配合適的可見度( private, protected ),不要讓所有的方法都是 public (這是默認(rèn)設(shè)定)。這是 Ruby 不是 Python。

    public, protected, 和 private 等可見性關(guān)鍵字應(yīng)該和其(指定)的方法具有相同的縮進(jìn)。并且不同的可見性關(guān)鍵字之間留一個空格。

   

 class SomeClass
   def public_method
    # ...
   end

   private

   def private_method
    # ...
   end

   def another_private_method
    # ...
   end
  end

    使用 def self.method 來定義單例方法. 當(dāng)代碼重構(gòu)時, 這將使得代碼更加容易因為類名是不重復(fù)的.

  class TestClass
   # bad
   def TestClass.some_method
    # body omitted
   end

   # good
   def self.some_other_method
    # body omitted
   end

   # Also possible and convenient when you
   # have to define many singleton methods.
   class << self
    def first_method
     # body omitted
    end

    def second_method_etc
     # body omitted
    end
   end
  end

  class SingletonTest
   def size
    25
   end
  end

  test1 = SingletonTest.new
  test2 = SingletonTest.new
  def test2.size
   10
  end
  test1.size # => 25
  test2.size # => 10

    本例中,test1 與 test2 屬於同一類別,但 test2 具有重新定義的 size 方法,因此兩者的行為會不一樣。只給予單一物件的方法稱為單例方法 (singleton method)。

相關(guān)文章

  • 詳細(xì)解析Ruby中的變量

    詳細(xì)解析Ruby中的變量

    這篇文章主要介紹了詳細(xì)解析Ruby中的變量,是Ruby學(xué)習(xí)中最基礎(chǔ)的知識之一,需要的朋友可以參考下
    2015-05-05
  • Ruby版本管理工具RVM的安裝和使用教程

    Ruby版本管理工具RVM的安裝和使用教程

    這篇文章主要介紹了Ruby版本管理工具RVM的安裝和使用教程,本文示例基于類Unix的系統(tǒng)環(huán)境,需要的朋友可以參考下
    2015-08-08
  • Ruby中相等性判斷的4種方法

    Ruby中相等性判斷的4種方法

    這篇文章主要介紹了Ruby中相等性判斷的4種方法,本文講解了“==” 最常見的相等性判斷、“===” 用于 case 語句的相容判斷、“equal?” 相同對象判斷、“eql?” 對象 hash 值判斷等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • rails常用數(shù)據(jù)庫查詢操作、方法淺析

    rails常用數(shù)據(jù)庫查詢操作、方法淺析

    這篇文章主要介紹了rails常用數(shù)據(jù)庫查詢操作、方法淺析,總結(jié)的比較全,WEB開發(fā)種常用的數(shù)據(jù)庫操作都列出了rails對應(yīng)代碼,需要的朋友可以參考下
    2014-06-06
  • Ruby一行代碼實現(xiàn)的快速排序

    Ruby一行代碼實現(xiàn)的快速排序

    這篇文章主要介紹了Ruby一行代碼實現(xiàn)的快速排序,本文直接給出實現(xiàn)代碼,超級簡潔的一個的方法,需要的朋友可以參考下
    2015-05-05
  • win7下從ruby源代碼編譯安裝的方法

    win7下從ruby源代碼編譯安裝的方法

    下面小編就為大家?guī)硪黄獁in7下從ruby源代碼編譯安裝的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Ruby程序中正則表達(dá)式的基本使用教程

    Ruby程序中正則表達(dá)式的基本使用教程

    和Python與Perl一樣,Ruby對正則表達(dá)式的支持也是相當(dāng)好的,這里送出整理的Ruby程序中正則表達(dá)式的基本使用教程,需要的朋友可以參考下
    2016-05-05
  • 深入講解Ruby中Block代碼快的用法

    深入講解Ruby中Block代碼快的用法

    這篇文章主要介紹了深入講解Ruby中Block代碼快的用法,block是Ruby學(xué)習(xí)進(jìn)階當(dāng)中的重要知識,需要的朋友可以參考下
    2015-05-05
  • 對優(yōu)化Ruby on Rails性能的一些辦法的探究

    對優(yōu)化Ruby on Rails性能的一些辦法的探究

    這篇文章主要介紹了對優(yōu)化Ruby on Rails性能的一些辦法的一些探究,包括避免內(nèi)存密集型的應(yīng)用和GC等相關(guān)問題的探討,需要的朋友可以參考下
    2015-11-11
  • 詳解Ruby中的循環(huán)語句的用法

    詳解Ruby中的循環(huán)語句的用法

    這篇文章主要介紹了詳解Ruby中的循環(huán)語句的用法,Ruby中的循環(huán)語句與其他編程語言的相比之下顯得有所不同,需要的朋友可以參考下
    2015-04-04

最新評論

镇江市| 雷波县| 保康县| 清镇市| 黄陵县| 连南| 阿合奇县| 北辰区| 关岭| 桃园市| 忻州市| 保德县| 新河县| 湾仔区| 长沙县| 桦甸市| 林芝县| 平和县| 阿克苏市| 南澳县| 衡水市| 靖西县| 新民市| 南木林县| 泉州市| 开江县| 桦川县| 三亚市| 大安市| 邵东县| 吴忠市| 阿拉善右旗| 墨江| 龙口市| 江口县| 哈巴河县| 白城市| 开化县| 双峰县| 凤冈县| 陇南市|