我正在执行rake文件中的任务。
# gottaRunThis.rake
task [:var] => :environment do |t, args|
Foo::Bar::Bell::this_function(args.var)
#runs Bell::this_function(var) but fails to complete.
# errors out with the words:
# NoMethodError: undefined method `put' for nil:NilClass
# Because Bell needs a @connection variable that doesn't exist in
# it's context when called directly.
end
问题是,目标模块和def应该包含在另一个类中。
# mainclass.rb
module Foo
module Bar
class TheMainClass
include Foo::Bar::Bell
def initialize
@site = A_STATIC_SITE_VARIABLE
@connection = self.connection
end
def connection
# Connection info here, too verbose to type up
end
end
end
end
Bell
看起来像
# bell.rb
module Foo
module Bar
module Bell
def do_the_thing(var)
#things happen here
#var converted to something here
response = @connection.put "/some/restful/interface, var_converted
end
end
end
是否应该修改Bell
,使其包含TheMainClass
? (如果是这样,我不知道如何?)还是应该在我的rake文件中使用一些语法,例如
Foo::Bar::TheMainClass::Bell::do_the_thing(args.var)
#I don't think this works... should it? What would the equivilent but working version look like?
答案 0 :(得分:1)
包含的方法可用作实例方法,因此您可以像这样调用do_the_thing
:
main_class = Foo::Bar::TheMainClass.new
main_class.do_the_thing(args.var)