草庐IT

find_module

全部标签

ruby - Class.superclass = 模块,Module.class = 类?

这是如何计算的?是圆形的更新:(在irb中)Class.superclass=ModuleModule.class=Class当Class是Modules的下类时,怎么可能说Module的类是Class?它是圆形的,先有鸡还是先有蛋。对象:同样的问题:对象是对象模型中的根对象。它的类怎么可能是Class,因为Class对象还不存在呢? 最佳答案 我们来看一下MRI源码的class.c文件:voidInit_class_hierarchy(void){id_attached=rb_intern("__attached__");rb_c

ruby - 使用 "::"而不是 "module ..."用于 Ruby 命名空间

在Ruby中,编写classFoo::Bar和编写moduleFoo有区别吗?类Bar用于命名空间?如果是,是什么? 最佳答案 如果你使用classFoo::Bar,但是Foo模块还没有被定义,一个异常将会被抛出,而moduleFoo;如果尚未定义,类Bar方法将定义Foo。此外,使用block格式,您可以在以下范围内定义多个类:moduleFooclassBar;endclassBaz;endend 关于ruby-使用"::"而不是"module..."用于Ruby命名空间,我们在St

ruby - 为什么没有 Module.method_defined?(:method) work correctly?

我正在尝试使用Module.method_defined?(:method)检查模块中是否定义了方法,它返回false,应该返回true。moduleSomethingdefself.another1endendSomething.methods列出了“另一个”,但Something.method_defined?(:another)返回false。这可能不起作用,因为该方法是在self上定义的吗?如果是这种情况,除了使用method_defined?之外,还有其他方法可以检查模块上是否定义了方法吗? 最佳答案 要知道模块是否有模块

ruby - rbenv — 'find_spec_for_exe':找不到带有可执行包的 gem 打包器 (>= 0.a) (Gem::GemNotFoundException)

我是rbenv的新手(长期使用RVM)。今天我彻底卸载了我的RVM并安装了rbenv。我成功安装了Ruby2.5.1,没有任何问题。但是,当我今天尝试为一个项目运行bundleinstall时,出现了以下错误:'find_spec_for_exe':找不到带有可执行包(Gem::GemNotFoundException)的gem打包器(>=0.a)我的bundler安装似乎出了点问题,但我不确定哪里出了问题。使用sudo运行geminstallbundler无法解决问题。这是完整的轨迹:$echo$SHELL/bin/zsh$cat~/.gemrcgem:--no-documentin

ruby-on-rails - rails : dynamically define class method based on parent class name within module/concern

我想根据包含此Mixin的类名在Mixin中动态生成一个类方法。这是我当前的代码:moduleMyModuleextendActiveSupport::Concern#defsome_methods#...#endmoduleClassMethods#HereiswhereI'mstuck...define_method"#{self.name.downcase}_status"do#dosomething...endendendclassMyClass但这给了我以下方法名称:MyClass.mymodule::classmethods_status在方法定义中获取基类名称是可行的(s

ruby-on-rails - rails ActiveRecord : Multiple conditions in find

这可能更像是一种Ruby语法。我很难在SomeObject.find上获得两个限制条件。分离,条件似乎可行:ifsearch!=''find(:all,:conditions=>['nameLIKE?',"%#{search}%"])elsefind(:all,:conditions=>['active',1]).shuffleend第一个案例我要的是:find(:all,:conditions=>['nameLIKE?',"%#{search}%"],['active',1])但是该行抛出语法错误,意外的')',期待tASSOC。 最佳答案

ruby-on-rails - 安装 ruby​​ gems 后,运行新的 gem 返回 "Could not find"错误

全新的Ubuntu10.04安装:我在让我的gem安装过程正常运行时遇到了一些问题。每当我安装gem时,它都会通过一个漂亮、直接的过程,没有任何问题:$geminstallrailsSuccessfullyinstalledrails-3.2.21geminstalled然后验证版本,也许是一个简单的“rails-v”?不:$rails-v/home/pragone/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in`to_specs':Couldnotfindrailti

ruby-on-rails - find_or_initialize_by 不适用于 2 列

我正在尝试使用ActiveRecord在我的数据库中插入数据。当我使用pdays=MyModel.new而不是下面的find_or_initialize_by进行初始化时,脚本运行良好。但它只运行一次。我需要它每天运行以执行更新。当我第二次尝试使用pdays=MyModel.new运行脚本时,没有出现重复键约束。因此,我正在尝试使用以下带有2个参数的find_or_initialize_by,但这给出了一个错误:undefinedmethod`find_or_initialize_by'2列一起构成唯一记录:vertica_traffic.eachdo|vdays|pdays=MyMo

ruby-on-rails - 让 find_by in rails 返回多个结果

我正在尝试返回用户创建的所有组。所有组都与用户标识相关联。当我运行find_by查询时,它只返回第一个结果。有没有办法让它返回多个?提前致谢 最佳答案 我正在写一个单独的答案,因为我没有50分,无法评论JamesLowrey的答案。find_all_by已弃用(Ruby4.2)。要从模型中获取事件记录列表,请执行以下操作:Model.where(attribute_name:val)例如,要在Vehicle表(具有列名称“model_name”)中查找所有记录,使得model_name的值为“Audi”,执行@vehicles=Ve

ruby-on-rails - 助手设计 : could not find the `Warden::Proxy` instance on request environment

我尝试将Devise用于我的Rails应用程序。我可以注册并登录,但是当我转到其他页面“构建”时,出现以下错误:Devise::MissingWardeninHome#showDevisecouldnotfindtheWarden::Proxyinstanceonyourrequestenvironment.MakesurethatyourapplicationisloadingDeviseandWardenasexpectedandthattheWarden::Managermiddlewareispresentinyourmiddlewarestack.Ifyouareseeing