13

I have the HTTParty gem on my system and I can use it from within rails.

Now I want to use it standalone.

I am trying:

class Stuff
  include HTTParty
  def self.y
    HTTParty.get('http://www.google.com')
  end 
end
Stuff.y

but I get

$ ruby test_httparty.rb 
test_httparty.rb:2:in `<class:Stuff>': uninitialized constant Stuff::HTTParty (NameError)
        from test_httparty.rb:1:in `<main>'
07:46:52 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker 73845718_get_method
$ 
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

2 Answers2

25

You have to require 'httparty':

require 'httparty'

class Stuff
  include HTTParty
  # ...
end
Stefan
  • 109,145
  • 14
  • 143
  • 218
-2

Its all because of the include which exists with in the class

If you include a class with a module, that means you're "bringing in" the module's methods as instance methods.

If you need more clarity on include and require

I request you to refer to this wonderful SO Posting

What is the difference between include and require in Ruby?

Here is an example which I have taken from the same posting

 module A
   def say
     puts "this is module A"
   end
 end

 class B
   include A
 end

 class C
   extend A
 end
B.say => undefined method 'say' for B:Class

B.new.say => this is module A

C.say => this is module A

C.new.say => undefined method 'say' for C:Class
Community
  • 1
  • 1
Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
  • @JörgWMittag I am sorry if it not meeting the requirement , I posted it because , the person posted the question wouldn't have asked this if he is aware of this issue . – Aravind.HU Jun 25 '14 at 12:54