我这样使用a library并且工作正常:
# app/bot/bot.rb
require 'facebook/messenger'
include Facebook::Messenger
Facebook::Messenger::Profile.set(...) # stuff
Bot.on(:message){|message| ... } # stuff
为了保持整洁,我决定将所有内容保存在课堂上,如下:
# app/bot/bot.rb
require 'facebook/messenger'
include Facebook::Messenger
class NeatBot
def initialize
setup_bot
Bot.on(:message){ |msg| receive_message(msg) }
end
private
def setup_bot
Facebook::Messenger::Profile.set(...) # <-- no longer works
end
def receive_message(message)
# ...
end
end
# Instantiate bot
NeatBot.new
现在它不再起作用了:
初始化
中未初始化的常量Facebook :: Messenger :: Profile(NameError)
许多类方法将使用Facebook::Messenger::
方法。我怎样才能让它发挥作用?
答案 0 :(得分:0)
include Facebook::Messenger
实际上将此模块中的所有方法“加载”为类实例方法。常量和类也包括在内。所以这个方法在类实例范围内都可用。你也写了这样的东西:
require 'facebook/messenger'
class CleanBot
include Facebook::Messenger
def initialize
setup_bot
Bot.on(:message){ |msg| receive_message(msg) }
end
private
def setup_bot
Profile.set(...) # stuff
end
def receive_message(message)
# ...
end
end