猴子補丁(Monkey Patch)是一種程式設計技巧,可以用來在執行時動態的修改(擴充套件)類別或模組,我們可以通過新增 Monkey Patch 來修改並滿足自己需求。
看以下例子,我們可以直接在Array的個類別裡面增加方法。
從輸出結果來看,我們藉由(Monkey Patch)這種技巧,在Array類別上增加了monkey1的個方法,也能正常輸出結果。
在ruby裡有一個變數叫做區域變數,看以下例子,先定義一個Father的類別,而在裡面建立一個叫做initialize的方法,再來就是把Father這個類別實體化f1。
我們來測試Ruby裡面的Public、private、Protected有哪些不同。
之前打了ㄧ篇文章叫Rails=>Method和Block,這篇的開頭打錯應該是Ruby才對而不是Rails。
我們之前有說道Proc和block之間的關係,這邊要來說Proc和lambda之間的關係。
在ruby裡self指的是代表,當前指向的物件,這句話可能不是很好理解,我們可以用下面的例子來實驗。