iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 4
3
Modern Web

30天修煉Ruby面試精選30題系列 第 4

Day04 - 玩弄Ruby的方法: instance method與class method

  • 分享至 

  • twitterImage
  •  

前情提要

第三天時,我們解說了如何在class裡用includeextend,去使用modulemethod

Include is for adding methods to an instance of a class.
Extend is for adding class methods. (出處)
...Also, it is sometimes ok to use "include" to add both instance and class methods. # 這句話比較進階,之後再研究:)

並透過圖書館模組的程式碼明白這段話的含義:

include是把類別中的物件實體加上方法

extend是用於類別方法

module Library
  def IThelp
    p "IThelp helps me!"
  end
end

class NewbieLearnsRuby
  include Library
end

class ExtendRuby
  extend Library
end

NewbieLearnsRuby.new.IThelp
# IThelp helps me!

ExtendRuby.IThelp
# IThelp helps me!

話不多說,進入今天的章節:


Ruby經典面試題目 #04

解釋實體方法與類別方法 Explain instance method and class method.

類別方法class method

為了瞭解類別方法,我們今天要建立新的類別class:鐵人賽名單IronmanList,讓這個class利用find方法,以傳入的id值順利找到某位鐵人賽的參賽者:

class IronmanList
  class << self
    def find(id)
    p "finding Ironman ID: #{id}"
    end
  end
end

IronmanList.find(1)
# finding Ironman ID: 1

當傳入1id,會使IronmanList這個類別,印出finding Ironman ID: 1

以上的程式代表,當接收者不是物件object,而是類別class本身,就是一個類別方法class method

這邊的<<指的是push方法,用在class method,意思是將self method push到 類別class裡。

鐵人賽名單class也可寫為:

class IronmanList
  #class << self
    def self.find(id) #在這裡的self is a class Method
    p "finding Ironman ID: #{id}"
    end
  #end
end

IronmanList.find(1)

我們把 class << self ... end 這部分都用註解消掉,直接使用self這個class method,讓 self.find(id)與之前呈現出一樣的結果!

使用class method的情況

當我們要寫class method時,如果此方法並不會和某個特定的實體變數綁在一起,就該使用類別方法!

實體方法(instance method)

把鐵人賽名單類別擴充一下,除了find方法,還有ironmanwinner方法

class IronmanList

  def self.find(id)
    p "finding Ironman ID: #{id}"
  end

  def ironmanwinner
    p "I've got a trophy!"
  end

end

IronmanList.find(1) #這是類別方法
IronmanList.new.ironmanwinner #這是實體方法

結果會印出:

finding Ironman ID: 1
I've got a trophy!

使用instance method的情況

如果你需要將實體方法,運用在某個客製化的實體。

This is often when the functionality concerns the identity of the instance such as calling properties on the object, or invoking behaviour.出處

如同鐵人賽的贏家不會只有一個名額,只要能自我挑戰成功,都能練成鐵人:)。
因此我們可以再new更多的物件,盡情使用這個ironmanwinner實例方法:

class IronmanList

  def self.find(id)
  p "finding Ironman ID: #{id}"
  end


  def ironmanwinner
    p "I've got a trophy!"
  end

end
# IronmanList.find(1)

Ting = IronmanList.new
Ting.ironmanwinner

Bater = IronmanList.new
Bater.ironmanwinner

結果印出:

"I've got a trophy!"
"I've got a trophy!"

例子不會只有一種,解釋方法更不會只有一種。我們除了用自己寫的程式碼理解概念,近一步拿關鍵字 instance method class method ruby去請教Google大神透過網路這座大圖書館,其他工程師們的部落格文章、透過各種文字說明與舉例加深我們的印象。看到排名第一的解釋寫著:

Class can use methods from three areas:

  1. Instances of Class have access to the instance methods defined in Module
  2. Instances of class can call methods that are defined as instance methods in their class.
  3. Or instances can call a singleton method of a class object. 出處

類別可以使用三種方法:

  1. 類別的物件實體,去存取模組裡的實體方法。(例如:第二天LibraryModule所寫的的IThelp Method)
  2. 類別的物件實體,用自己類別裡定義的實體方法。(例如:本日第四天Ironmanlist Method)
  3. 類別可以呼叫類別物件的單例方法(singleton method)。(例如:本日第四天IronmanListClass的self.find(id) Method)

這裡又發現一個新名詞了:singleton method,這將成為我們下一篇的素材呢!

=欲知詳情,下回分解!=

Ref:


上一篇
Day03 - Ruby比一比: Module的include與extend
下一篇
Day05 - 尋找自己: Ruby的self物件與singleton method
系列文
30天修煉Ruby面試精選30題31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
Bater
iT邦新手 4 級 ‧ 2018-10-07 20:04:58

這是非常重要的觀念喔!

我也深有同感,寫完鐵人賽文章之後的印象更深了!

我要留言

立即登入留言