在 class 定義裡使用一致的結構。
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。類別應該只在建立執行個體是合理的時候使用。
# 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
當你希望將模組的執行個體方法變成 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.
在設計類層次的時候確保他們符合 Liskov Substitution Principle 原則。(譯者注: LSP原則大概含義為: 如果一個函數中引用了 父類的執行個體, 則一定可以使用其子類的執行個體替代, 並且函數的準系統不變. (雖然功能允許被擴充))
Liskov替換原則:子類型必須能夠替換它們的基底類型 <br/>
1. 如果每一個類型為T1的對象o1,都有類型為T2的對象o2,使得以T1定義的所有程式P在所有的對象o1都代換為o2時,程式P的行為沒有變化,那麼類型T2是類型T1的子類型。 <br/>
2. 換言之,一個軟體實體如果使用的是一個基類的話,那麼一定適用於其子類,而且它根本不能察覺出基類對象和子類對象的區別。只有衍生類替換基類的同時軟體實體的功能沒有發生變化,基類才能真正被複用。 <br/>
3. 裡氏代換原則由Barbar Liskov(芭芭拉.裡氏)提出,是繼承複用的基石。 <br/>
4. 一個繼承是否符合裡氏代換原則,可以判斷該繼承是否合理(是否隱藏有缺陷)。
努力使你的類儘可能的健壯 [SOLID](http://en.wikipedia.org/wiki/SOLID_object-oriented_design\))。(
總是為你自己的類提供 to_s 方法, 用來表現這個類(執行個體)對象包含的對象.
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(建構函式) 和 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),構造器(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 - 它已經是一個新的 class。擴充它會產生一個多餘的 class 層級
並且可能會產生怪異的錯誤如果檔案被載入多次。
考慮添加Factory 方法來提供靈活的方法來建立特定類執行個體。
class Person def self.create(potions_hash) # body omitted end end
鴨子類型(duck-typing)優於繼承。
# 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.
避免使用類變數(@@)因為他們討厭的繼承習慣(在子類中也可以修改父類的類變數)。
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"
正如上例看到的, 所有的子類共用類變數, 並且可以直接修改類變數,此時使用類執行個體變數是更好的主意.
根據方法的用途為他們分配合適的可見度( private, protected ),不要讓所有的方法都是 public (這是預設設定)。這是 Ruby 不是 Python。
public, protected, 和 private 等可見度關鍵字應該和其(指定)的方法具有相同的縮排。並且不同的可見度關鍵字之間留一個空格。
class SomeClass def public_method # ... end private def private_method # ... end def another_private_method # ... end end
使用 def self.method 來定義單例方法. 當代碼重構時, 這將使得代碼更加容易因為類名是不重複的.
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)。