2014-03-05 30 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")