2014-03-05 33 views
0

我有這樣如何在ruby中初始化一個模塊類?

module urlfetch 
    class fetch 

    def initialize(url) 
     @url = url 
     analyze! 
    end 

    #extra methods goes here 

    end 
end 

模塊我嘗試這樣

response = urlfetch::fetch("http://google.com") 
puts response 

,但我發現錯誤,如undefined method fetch

回答

4

類和模塊在紅寶石由大寫名稱定義,以便

module Urlfetch 
    class Fetch 

    def initialize(url) 
     @url = url 
     analyze! 
    end 

    #extra methods goes here 

    end 
end 

則經由new方法初始化類

response = Urlfetch::Fetch.new("http://google.com") 
puts response 
2

首先,模塊和類應當資本化,作爲常數。其次,你需要new來構造一個對象。

URLFetch::Fetch.new("http://google.com")