草庐IT

javascript - 将 angularjs 模块导出为 es6 模块

全部标签

ruby-on-rails - 在rails app目录中将文件夹作为模块常量加载的方法

所以有一个Rails5项目并且想要加载这样的目录/app/services/userfoo.rb作为常量::Services::User::Foo有没有人有使用Rails自动加载路径以这种方式加载常量的经验?foo.rbmoduleServicesmoduleUserclassFooendendend解决方案将此添加到您的application.rb文件config.autoload_paths在此处查看有关自动加载的讨论https://github.com/rails/rails/issues/14382#issuecomment-37763348https://github.com

ruby - 模块的实例变量是否在类与 mixin 之间共享?

我想知道Ruby模块的实例变量在多个类中的行为如何“混合”它。我写了一个示例代码来测试它:#HereisamoduleIcreatedwithoneinstancevariableandtwoinstancemethods.moduleSharedVar@color='red'defchange_color(new_color)@color=new_colorenddefshow_colorputs@colorendendclassExample1includeSharedVardefinitialize(name)@name=nameendendclassExample2includ

ruby - 需要模块内的文件?

我在某人的仓库中看到了以下源代码:moduleTwittermoduleBootstrapmoduleRailsrequire'twitter/bootstrap/rails/engine'ifdefined?(Rails)endendendrequire'less-rails'require'twitter/bootstrap/rails/bootstrap'ifdefined?(Rails)Source我想知道当我们将require放在模块中时有什么不同? 最佳答案 就require而言没有区别,即require总是将文件加载到

ruby-on-rails - 如何找出哪些模块是 Rails 中某个类的混入?

我是Ruby/Rails新手。这是一个令我困惑的问题:我们能否从API文档中找到Rails中某个类的确切模块列表混合?例如,如果我们有一个ActiveRecord::Base子类的实例,我们可以在这个类中使用validates方法,如下所示:classProducttrueend从railsapi文档我们可以发现validates属于ActiveModel::Validations::ClassMethods,所以ActiveRecore::Base必须有ActiveModel::Validations::ClassMethodsmixin,但我没有在接口(interface)引用。谁

ruby-on-rails - 是否可以反转类中包含的模块?

您将模块包含在类中,以在向该特定类添加类方法和实例方法方面扩展类功能。moduleMdefself.class_method_from_module'fromclass_method_from_module'enddefinstance_method_from_module'frominstance_method_from_module'endendclassCincludeMdefself.class_method'fromclass_method'enddefinstance_method'frominstance_method'endendputsC.class_method=>

ruby - 没有类的模块中实例方法的目的是什么?

设想以下Ruby模块:moduleFoodefinst_methodputs"CalledFoo.inst_method"enddefself.class_methodputs"CalledFoo.class_method"endend显然Foo.class_method可以在没有任何类实例的情况下被调用。但是,Foo.inst_method发生了什么?是否可以在不包含/扩展类的情况下调用Foo.inst_method?免责声明:问题的重点不是解决实际问题。我只是想提高我对Ruby对象系统的理解。 最佳答案 模块中实例方法的主要目的

ruby-on-rails - YAML::load 引发未定义的类/模块错误

我正在我的Rails应用程序中实现类似文件跟踪的功能。为此,我在YAML中序列化对象。我有item_at_version方法,它基本上执行YAML::load(cached_object)–这工作得很好,但是,我不知道为什么,有时它返回未定义的类/模块_类名_。它适用于Event、Conversation、Note等模型,但没有任何理由,它似乎会为Dataset、Comment、Student等模型抛出该错误(我试图找到任何模式,但没有任何运气).我正在使用rails3.2.8、ruby1.9.3p327、psych作为YAML引擎(Psych::Version返回1.3.4)。附言。

ruby - 我们什么时候使用 ruby​​ 模块 vs 使用类组合?

以前有人问过与此类似的问题,但我特别询问有关使用组合作为使用模块混入的替代方法的问题。classHelperdefdo_somthingendend如果我需要“使用”一个类而不是继承它,我会简单地组合它并使用它。classMyStuffdefinitializehelper=Helper.newhelper.do_somethingendend我为什么要为此创建一个模块:moduleHelperdefdo_somethingendendclassMyStuffincludeHelperend我看到的唯一区别是,如果我使用模块,周围不会有很多Helper对象。但是我没有看到任何东西周围有

ruby-on-rails - 事件管理员 CSV 导出自定义查询范围

我正在使用活跃的管理员导出CSV选项。它返回与特定表相关的所有值。我只想要特定月份的报告。有人能帮忙吗? 最佳答案 您可以编写自己的csv导出器collection_action:download_report,:method=>:getdousers=User.where('created_at>=?',Date.today-1.month)csv=CSV.generate(encoding:'Windows-1251')do|csv|#addheaderscsv:download_report))endindex:downloa

ruby - 何时在模块的方法中使用 self

我的模块定义如下:moduleRG::Statsdefself.sum(a,args={})a.inject(0){|accum,i|accum+i}endend要使用这个方法,我只需要包含这个定义的文件,这样我就可以:RG::Stats.sum(array)还有RG::Stats.method(:sum)但是,如果我需要使用RG::Stats.instance_methods了解方法列表,我会得到一个空数组。这是因为我使用了self。如果我省略self,则RG::Stats.instance_methods会给出方法列表,但我无法再访问它们。问题是:如何在模块的方法定义中使用self