我的系统上有HTTParty gem,我可以在rails中使用它。
现在我想独立使用它。
我在尝试:
class Stuff
include HTTParty
def self.y
HTTParty.get('http://www.google.com')
end
end
Stuff.y
但我得到
$ 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
$
答案 0 :(得分:15)
您必须require 'httparty'
:
require 'httparty'
class Stuff
include HTTParty
# ...
end
答案 1 :(得分:-2)
这完全是因为类
中存在的包含如果你包含一个带有模块的课程,那就意味着你要带来一个&#34;带来&#34;模块的方法作为实例方法。
如果您需要更清晰的include和require
我请你参考这个精彩的SO帖子
What is the difference between include and require in Ruby?
以下是我从同一篇文章中取得的一个例子
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