跳到主要內容

Ruby: module, include, extend, self, module_function

module

一個最簡單的module

module Foo
def foo
puts 'hello foo'
end
end
view raw module.rb hosted with ❤ by GitHub

include

在class使用include, 可以讓module內的方法為class的物件方法,在ruby稱為mixin

module Foo
def foo
puts 'foo'
end
end
class Bar
include Foo
end
bar = Bar.new
bar.foo # 印出hello foo (foo已成為Bar的物件方法)
view raw include.rb hosted with ❤ by GitHub

include可用在模擬多重繼承,因Ruby不像C++可以直接多重繼承,所以宣告為module,在mixin到class裡面

extend

在class使用extend,可以讓module內的方法為class的類別方法

與include的不同點在於

  • include: module內的方法為class的物件方法
  • extend: module內的方法為class的類別方法
module Foo
  def foo
    puts 'foo'
  end
end

class Bar
  extend Foo
end

Bar.foo  # 印出hello foo (foo已成為Bar的類別法)

self in module

self是什麼?就是呼叫Ruby當前物件本身指標所指的,在主程序直接呼叫self返回main。

先看最簡單的module

module Foo
end

直接呼叫self會出現什麼呢?

module Foo
  p self #印出 Foo
end

別忘了ruby式腳本式的語言,這個程式沒寫錯,單純只是宣告module,他裡頭的命令句還是被執行,所以印出Foo

那麼這個Foo物件是什麼?簡單,印出來看看

module Foo
  p self.class #印出Module
end

非常清楚,Ruby號稱所有東西都是物件,所以module也是物件,所以self的使用很清楚了,直接動態的在Foo物件上面加上方法

module Foo
  def self.foo
    puts 'hello foo'
  end
end

Foo.foo # 印出hello foo, 直接呼叫Foo這個Module形態的物件,裡頭的foo方法

所以同理,class裡面的self也是一樣的應用,class也是可以實體化為物件,並動態的加上方法,通常稱為類別方法,這種類別方法,在其他程式語言裡面也很常用,只是實作方式不同,Ruby是把class也當作物件處理

extend self

綜合上面所說的,如果module裡面 extend self會發生什麼事呢?

module Foo
  extend self

  def foo
    puts 'hello foo'
  end
end

Foo.foo # 印出hello foo

如果在class內使用extend會使module內的方法,成為類別方法,那如果在module使用,同樣會成為module方法,這個講法並不好,因為在Ruby裡面都是物件,只是在其他語言稱為類別方法,所以我照著命名

extend self如果有點難懂,先看extend 別的模組

module Foo
  def foo
    puts 'hello foo'
  end
end

module Bar
  extend Foo
end

Bar.foo # 印出hello foo (foo成為bar的類別方法)

就在跟class裡面extend一樣。同理extend self,就可以讓自己本身的方法,可以用module方法的方式呼叫,也可以被class include時,被當成物件方法呼叫

module Foo
extend self
def foo
puts 'hello foo'
end
end
class Brb
include Foo
end
Foo.foo # 印出hello foo(直接當module方法呼叫)
brb = Brb.new
brb.foo # 印出hello foo(當brb的物件方法呼叫)

module_function

從名稱得知,使用module_function,則只能當成module function來使用

將要變成module function的方法名稱,使用symbol的方式傳入

module Foo
  def foo
    puts 'hello foo'
  end

  module_function :foo
end

Foo.foo  印出hello foo(變成module方法)

跟上面extend self不同的是,它就只能當module方法,如果在class內include,也不會變成物件方法

module Foo
  def foo
    puts 'hello foo'
  end

  module_function :foo
end

class Brb
  include Foo
end

Foo.foo # 印出hello foo
brb = Brb.new
brb.foo # 跳出NoMethodError錯誤,因為foo只能當module方法,不是當物件方法來呼叫

included

還有一個動態的方法,讓class include時,再決定哪些是類別方法,哪些是物件方法

module內建一個方法叫做include,可以在該module被include時被呼叫,舉個例子

module Foo
  def self.included(receiver)
    puts "#{self}被#{receiver} include進去了"
  end
end

class Brb
  include Foo #印出Foo被Brb include進去了
end

所以我們可以在module內決定include它的class要哪些類別方法,幫它(class)呼叫extend就行了,例如

module Bar
  def bar
    puts 'hello bar'
  end
end

module Foo
  def self.included(receiver)
    receiver.extend Bar # 幫它(class)呼叫extend 並extend Bar進去
  end
end

class Brb
    include Foo
end

Brb.bar # 印出hello bar

同理,也可以幫它呼叫include

module Bar
  def bar
    puts 'hello bar'
  end
end

module Foo
  def self.included(receiver)
    receiver.send :include, Bar # 幫它(class)呼叫include 並include Bar進去
  end
end

class Brb
    include Foo
end

brb = Brb.new
brb.bar # 印出hello bar

也可以在module直接嵌套在module裡面 讓程式馬更簡潔,下面示範嵌套完,並直接呼叫extend與include

module Foo
  module Bar # 將用來當類別方法
      def bar
        puts 'hello bar'
    end
  end

  module Bla # 將用來當物件方法
    def bla
        puts 'hello bla'
    end
  end

  def self.included(receiver)
    receiver.extend Bar # 將Bar設定為類別方法
    receiver.send :include, Bla # 將bla設定為物件方法
  end
end

class Brb
    include Foo
end

Brb.bar
brb = Brb.new
brb.bla

留言

這個網誌中的熱門文章

HTML, CSS, 相對視窗或螢幕的高度與寬度

在 w3school.com 網站, CSS Units 有各種與寬度的表示法 以我使用的頻率來排序: px: 使用螢幕幾個像素(但是還要考慮Retina螢幕像素是一般螢幕像素的兩倍) %: 相對父層的大小比例 vh, vw: 相對於瀏覽器展示網頁區域的大小(不是整個瀏覽器的大小,沒包含瀏覽器的工具列,只有展示網頁的區域) vmin: vh, vw取最小值(另外還有vmax則是取最大值,但是目前IE跟safari不支援) px 與 % 很常用, vh , vw 與 vmin 是CSS3的新產物,表示相對瀏覽器展示頁面的大小 相對視窗大小 這邊先說明, vh , vw 與 vmin 只包含網頁顯示區域的長寬,不包含瀏覽器的工具列 先從dom的最根本講起好了,一份HTML文件,根是 <html></html> (雖然沒有嚴格規定,不寫也能顯示),然後這個根的父元件就是瀏覽器的網頁頁面顯示區 因此,如果對 <html></html> 宣告大小是 100% 就跟宣告 100vh 一樣,因為都是指瀏覽器的網頁頁面顯示區大小 這個範例是將html設定為100vh <html style="height: 100vh"> <head> <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript"> console.log($('html').height()); </script> </head> </html> 這個範例是將html設定為100% <html style="height: 100%"> <head> <script src="https://code.jquery.com/jque...

陣列洗牌程式(shuffle array)

陣列如何把它的順序打亂,作出類似洗牌的效果,我一直都很頭痛,搞得非常的複雜。至從用了Ruby,Array物件包含 shuffle 方法之後,我就沒思考過陣列洗牌的問題了,反正Ruby幫我處理得好好的。 Ruby # 52張牌的牌堆 poker = (1..52).to_a # => [1, 2, 3, ... , 52] # 洗牌打亂 shuffled = poker.shuffle # => [22, 32, 12, ... # 也可以直接打亂原來的陣列 poker.shuffle! # => [39, 47, 3, ... Javascript Javascript我就頭痛了,我得自己寫洗牌的方法。我在網路上找到了這個演算法,仔細看了之後,才知道原來洗牌可以這麼簡單: // 原本for迴圈是一行程式,太難理解,這邊改寫成多行 function shuffle(o){ for(var j, x, i = o.length; i;){ j = Math.floor(Math.random() * i); // javascript的array是0-base // 所以迴圈第一次進入,--i後表示陣列最後一個位置。 x = o[--i]; o[i] = o[j]; o[j] = x; // 以上三行代表以x為temp, o[i], o[j]做交換 } return o; //回傳陣列,我一開始也看錯看成回傳0 }; 變數說明 引數o: 將被洗牌的陣列 for迴圈內 i : 將會從陣列的最後一個位置,慢慢往前移到第一個位置(但移到第一個位置時for迴圈不執行,因為Javascript的數值0也代表false,會離開迴圈。0代表flase這點跟Ruby不一樣) j : 將會被亂數選擇,選到要被交換的位置 x : 用來暫存o[i]的數值,幫助o[i]與o[j]做數值交換 就這樣從最後一個位置開始,依次往前隨機挑選一個位置與它交換(可能挑到自己,表示不交換),來達到洗牌的效果,陣列多大,就執行幾次,時間複雜度 O(n) ,...

安裝zsh + oh-my-zsh 出現 /usr/bin/env: zsh: 沒有此一檔案或目錄 (/usr/bin/env: zsh -: No such file or directory)

zsh 跟 oh-my-zsh 安裝完,可能會出現類似下面錯誤訊息,雖然他沒任何影響,不過我總覺得怪怪的 # 中文系統 /usr/bin/env: zsh: 沒有此一檔案或目錄 # 英文系統 /usr/bin/env: zsh -: No such file or directory 通常會出現這類的訊息,都是指令找不到。 這邊指zsh這個指令找不到,試試看輸入 which zsh 看看是不是沒有把zsh這個加到 $PATH 裡面,沒有的話加入就好了。 如果加入了一樣找不到,我trace了一下zsh的執行過程,通常都是跟 oh-my-zsh 的安裝順序錯了的時候才會發生。 這時編輯家目錄底下檔案 vim ~/.zshrc ,會看到 # 他先執行了oh-my-zsh.sh source $ZSH/oh-my-zsh.sh # 然而oh-my-zsh.sh已經在使用zsh這個指令了 # 這裡才把宣告路徑,所以當然zsh指令找不到 export PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/bin:/bin:/root/bin:/usr/local/bin" 這時就把它們兩個對調一下就好了 # 像這樣把 export PATH 放到 source $ZSH/oh-my-zsh.sh 上面 export PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/bin:/bin:/root/bin:/usr/local/bin" source $ZSH/oh-my-zsh.sh # 這樣oh-my-zsh.sh裡面就可以用zsh指令了 存檔後,登出再進來,他就可以正常使用zsh指令,不再出現這種錯誤訊息了