原文:Ruby on Rails Guides — Active Storage Overview
轉貼自:https://calvertyang.github.io/2018/05/18/active-storage-overview/
原文:Ruby on Rails Guides — Active Storage Overview
轉貼自:https://calvertyang.github.io/2018/05/18/active-storage-overview/
循環循環(Loops & Iterators)讓你可以重複執行特定的程式碼。例如,當你要將同一個訊息重複輸出十次時,就可以用到循環循環。Ruby的循環循環主要可分成三組:
我們來逐一地看看吧。
def credit_card_discount
original_price * 0.9
end
class UserProfile
def initialize(name)
@name = name
end
end
class Invoice
CREDIT_CARD_TYPE = ["VISA","MASTER"]
end
!
,執行方式和一般方法沒有差別,但通常用!結尾,表示會有「副作用」,例如,這個方法會改變原本的變數值,同時也表示它會有一個相對應不會改變原本變數值的方法。?
,這個方法經常會回傳表示 true 或 false 的物件。=
,表示這個方法是指派方法(assignment method),在 assignment method 中,函式中的回傳值會被忽略,而是自動回傳參數的值。在 Rails 中會自動將 class 名稱轉成複數的形式到資料庫找相對應的資料表: