一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Ruby - Ruby中編寫類與模塊的風格指南

Ruby中編寫類與模塊的風格指南

2020-05-05 11:26腳本之家 Ruby

這篇文章主要介紹了Ruby中編寫類與模塊的風格指南,示例采用的也是Ruby編程中一些常用的編寫慣例,需要的朋友可以參考下

在 class 定義里使用一致的結構。

    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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。類別應該只在創(chuàng)建實例是合理的時候使用。

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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原則大概含義為: 如果一個函數(shù)中引用了 父類的實例, 則一定可以使用其子類的實例替代, 并且函數(shù)的基本功能不變. (雖然功能允許被擴展))

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

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

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

   

?
1
2
3
4
5
6
7
8
9
10
11
12
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 功能成員來定義各個實例變量的訪問器或者修改器方法。

  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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 作為替代。

?
1
2
3
4
5
6
7
# 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(構造函數(shù)) 和 comparison(比較) 操作。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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。擴展它會產(chǎn)生一個多余的 class 層級
    并且可能會產(chǎn)生怪異的錯誤如果文件被加載多次。

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

    

?
1
2
3
4
5
class Person
   def self.create(potions_hash)
    # body omitted
   end
  end

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

  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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.

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

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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 (這是默認設定)。這是 Ruby 不是 Python。

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

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SomeClass
  def public_method
   # ...
  end
 
  private
 
  def private_method
   # ...
  end
 
  def another_private_method
   # ...
  end
 end

    使用 def self.method 來定義單例方法. 當代碼重構時, 這將使得代碼更加容易因為類名是不重復的.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)。

 

延伸 · 閱讀

精彩推薦
  • RubyRuby迭代器的7種技巧分享

    Ruby迭代器的7種技巧分享

    這篇文章主要介紹了Ruby迭代器的7種技巧分享,Ruby中的迭代器非常人性化,本文既是講解了7個技巧也是講解了7種迭代器,需要的朋友可以參考下 ...

    腳本之家4782020-04-20
  • RubyRuby簡潔學習筆記(一):字符串、數(shù)字、類和對象

    Ruby簡潔學習筆記(一):字符串、數(shù)字、類和對象

    這篇文章主要介紹了Ruby簡潔學習筆記(一):字符串、數(shù)字、類和對象,本文是學習筆記第一篇,需要的朋友可以參考下 ...

    腳本之家2472020-04-20
  • RubyRuby設計模式編程中使用Builder建造者模式的實例

    Ruby設計模式編程中使用Builder建造者模式的實例

    這篇文章主要介紹了Ruby設計模式編程中使用Builder建造者模式的實例,建造者模式將一個復雜對象的構造與它的表示分離,使同樣的構建過程可以創(chuàng)建不同的表...

    范孝鵬2192020-05-07
  • RubyRuby進行文件信息輸出實例代碼

    Ruby進行文件信息輸出實例代碼

    Ruby進行文件信息輸出實例代碼,數(shù)據(jù)是隨機的,所以每次的記錄都會不同。 ...

    ruby教程網(wǎng)2962020-04-10
  • Ruby剖析 Ruby 訪問控制

    剖析 Ruby 訪問控制

    前面,我們說 Ruby 沒有函數(shù),只有方法.而且實際上有不止一種方法.這一節(jié)我們介紹 訪問控制 (accesscontrols). 想想當我們在最高層而不是在一個類的定義里定義...

    ruby教程網(wǎng)3572020-04-08
  • RubyRuby環(huán)境下安裝使用bundler來管理多版本的gem

    Ruby環(huán)境下安裝使用bundler來管理多版本的gem

    這篇文章主要介紹了Ruby環(huán)境下安裝使用bundler來管理多版本的gem的方法,舉了Ruby On Rails中的應用實例來進行演示,需要的朋友可以參考下 ...

    日拱一卒4332020-05-10
  • RubyCentOS中配置Ruby on Rails環(huán)境

    CentOS中配置Ruby on Rails環(huán)境

    經(jīng)過一個上午的折騰,終于把ROR環(huán)境在CentOS中搞定,繞了很多彎路,把文章寫下來總結一下 ...

    可樂加糖4762020-04-12
  • Ruby簡要說明Ruby中的迭代器

    簡要說明Ruby中的迭代器

    這篇文章主要介紹了Ruby中的迭代器,迭代器的概念在動態(tài)語言的編程中十分重要,文章中介紹了Ruby中的each迭代器和collect迭代器,需要的朋友可以參考下 ...

    goldensun2772020-04-25
主站蜘蛛池模板: 日本欧美一二三区色视频 | 免费特黄一级欧美大片在线看 | 国内久久精品 | 波多野结衣黑人系列在线观看 | 99热这里只有精品在线播放 | 国产高清国内精品福利 | 成人啪精品视频免费网站 | 800精品国产导航 | 免费人成网址在线观看国内 | 色老板在线播放 | 青柠网在线观看视频 | 99热精品在线免费观看 | 色哟哟哟在线精品观看视频 | 国产欧美曰韩一区二区三区 | 国产精品久久久久毛片真精品 | 视频在线欧美 | 逼逼流水了 | 高清一区高清二区视频 | 精品欧美一区二区精品久久 | 卫生间被教官做好爽HH视频 | 国产亚洲精aa在线观看不卡 | 91精品婷婷国产综合久久8 | 东北老妇露脸xxxxx | 1024国产基地永久免费 | 2018av在线 | 人皮高跟鞋在线观看 | 我的年轻漂亮继坶三级 | 性夜夜春夜夜爽AA片A | 国产福利视频一区二区微拍 | 91制片厂制作传媒网站 | 精品小视频在线 | 日本免费观看的视频在线 | 女人麻豆国产香蕉久久精品 | 91狠狠| 亚洲欧美日韩另类精品一区二区三区 | 日老逼 | а天堂中文最新版在线 | 国产99视频精品免费视频免里 | 包射屋 | 极品丝袜老师h系列全文阅读 | 国产日韩一区二区三区在线播放 |