PrivateUser
PrivateUser

Reputation: 4524

How to initialize a module class in ruby?

I have a module like this

module urlfetch
  class fetch

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

   #extra methods goes here

  end
end

I tried like this

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

But i'm getting error like undefined method fetch

Upvotes: 0

Views: 179

Answers (2)

bjhaid
bjhaid

Reputation: 9752

classes and modules in ruby are defined by uppercase names so

module Urlfetch
  class Fetch

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

   #extra methods goes here

  end
end

then you initialize a class via the new method

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

Upvotes: 4

Amadan
Amadan

Reputation: 198314

First off, modules and classes should be capitalised, as constants. Secondly, you need new to construct an object.

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

Upvotes: 2

Related Questions