我在lib目录中有模块 module Mymodule
def usefull_meth(a,b)
a+b
end
end
,在
application.rb
我想在我的应用开始时自动加载
我在config.autoload_paths += Dir["#{config.root}/lib/**/*"]
include Mymodule
但我仍然需要像usefull_meth(a,b)
我想在application helper
内使用我的usefull_meth
而不包含
我如何实现目标?或者我做错了什么?
我只是希望在我的助手中有Mymodule.usefull_meth
,我不需要 class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate {
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var downloadButton: UIButton!
@IBAction func downloadImage(sender: UIButton) {
let urlString = "https://img-fotki.yandex.ru/get/6111/8955119.3/0_7e1f6_a73b98a0_orig"
let url = NSURL(string: urlString)
var configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session: NSURLSession = NSURLSession(configuration: self.configuration)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
print("didReceiveData")
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
print("didReceiveRes")
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
let alert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "Ok", style: .Default, handler: nil)
alert.addAction(alertAction)
presentViewController(alert, animated: true, completion: nil)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
print("didReceiveSendData64")
var uploadProgress: Float = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
progressView.progress = uploadProgress
}
}
或其他
答案 0 :(得分:0)
但我仍然需要像包括Mymodule
那样包含它
这就是Ruby的工作原理,它与Rails自动加载无关。
为了全局使用某种方法,您应该将其放入全局命名空间。只需删除模块定义并在该文件中仅保留方法定义(即使我不明白为什么不直接将其放入帮助中)。
答案 1 :(得分:0)
我实现了我想要的目标
在application.rb
require './lib/my_module'
include Mymodule
现在我有了include Mymodule