草庐IT

外部接口调用

全部标签

ruby - 检查是否调用了类方法

我有以下模块和类:moduleMyModuledefself.includedbasebase.extend(ClassMethods)endmoduleClassMethodsattr_reader:config#thismethodMUSTbecalledbyeveryclasswhichincludesMyModuledefconfigure&block@config={}block.call(@config)ifblockendendendclassAincludeMyModuleconfiguredo|config|#dosthwiththeconfigendendclass

ruby - 从私有(private)实例方法调用私有(private)类方法

我是Ruby新手,来自C#世界。在C#中,这样做是合法的:publicclassTest{publicvoidMethod(){PrivateMethod();}privatevoidPrivateMethod(){PrivateStaticMethod();}privatestaticvoidPrivateStaticMethod(){}}是否可以在Ruby中做类似的事情?一些背景知识:我有一个Rails应用程序...其中一个模型有一个私有(private)方法来设置一些依赖项。有一个类方法可以创建模型的初始化实例。由于遗留原因,模型的某些实例未正确初始化。我添加了一个实例方法来初始

ruby - 如何通过 belongs_to 按外部 id 和本地属性进行过滤?

以下模型通过belongs_to链接:require'mongoid'classSensorincludeMongoid::Documentfield:sensor_id,type:Stringvalidates_uniqueness_of:sensor_idend...require'mongoid'require_relative'sensor.rb'classSensorDataincludeMongoid::Documentbelongs_to:sensorfield:date,type:Datefield:ozonMax1h,type:Floatfield:ozonMax8h

ruby - 为什么 ruby​​ 中的变量前缀允许在方法调用中省略括号?

在DavidFlanagan的TheRubyProgrammingLanguage中;松本幸弘theystatethatthevariableprefixes($,@,@@)areonepricewepayforbeingabletoomitparenthesesaroundmethodinvocations.谁可以给我解释一下这个? 最佳答案 这是我不成熟的意见。如果我错了,请纠正我。假设实例变量没有@前缀,那么我们如何声明一个实例变量?classMyClassdefinitialize#Herefooisaninstanceva

ruby - 什么是 "terminated object",为什么我不能调用它的方法?

我会定期收到此异常:NotImplementedError:method`at'calledonterminatedobject在这行代码中:nextifHpricot(html).at('a')这个错误是什么意思?我该如何避免? 最佳答案 您正在使用的库使用自定义C扩展。在C扩展中,它试图在已被垃圾回收的Ruby对象上调用方法。这在纯Ruby中是不可能发生的,因为垃圾收集器只会释放不再能从任何引用中访问的对象。但在C语言中,可能会在垃圾收集器不检查的地方保留对Ruby对象的引用(例如,编译器可能已将变量放入CPU寄存器中)。

ruby-on-rails - 使用 ApplicationController.renderer.render 从 Controller 外部渲染的 Rails 5 不会在自身上设置变量

我正在使用Rails5ApplicationController.renderer.render方法从模型中进行渲染。我需要将一些变量传递给我的布局,这是我使用locals选项完成的;如果直接访问此变量,则该变量在布局中可用,但不能通过self访问。这是我设置渲染的方式html_string=ApplicationController.renderer.render(file:"/#{template_path}/base/show",:formats=>[:pdf,:html],locals:{:@routing_form=>self,:controller_name=>contro

ruby - 从依赖属性中的 FactoryGirl 模型调用方法

我有一个类似于下面的模型:classFooattr_accessor:attribute_a#ReallyanActiveRecordattributeattr_accessor:attribute_b#AlsoanActiveRecordattributedefdetermine_attribute_bself.attribute_b=some_biz_logic(attribute_a)endend在FactoryGirl1.3中,我有一个看起来像这样的工厂:Factory.define:foodo|foo|foo.attribute_a="somerandomvalue"foo.

ruby - 调用 instance_eval(&lambda) 传递当前上下文时出现错误 'wrong number of arguments'

要清楚-此代码运行完美-codewithproc但如果我将Proc.new更改为lambda,则会出现错误ArgumentError:wrongnumberofarguments(1for0)这可能是因为instance_eval想要将self作为参数传递,而lambda将其视为一种方法并且不接受未知参数?有两个例子-第一个是工作:classRuledefget_ruleProc.new{putsname}endendclassPersonattr_accessor:namedefinit_rule@name="ruby"instance_eval(&Rule.new.get_rule

ruby-on-rails - 为什么 Controller 操作应该调用一个模型方法而不是初始查找或新方法?

我的模型中的函数几乎包含所有“共享”语句。问题是,当我需要在我的Controller中使用多个功能时,出现以下错误:ControlleractionshouldcallonemodelmethodotherthananinitialfindornewIDE会更深入地解释:Thisinspectionwarnsifacontrolleractioncontainsmorethanonemodelmethodcall,aftertheinitial.findor.new.It’srecommendedthatyouimplementallbusinesslogicinsidethemode

ruby-on-rails - 在调用 Rake 任务时设置多个环境变量

我可以像这样调用一个Rake任务并设置一个环境变量:$ONE=1raketemp:both但是如何设置两个环境变量呢?这行不通:$ONE=1TWO=2raketemp:both这行得通,但读起来很困惑:$ONE=1raketemp:bothTWO=2如何在调用rake之前传递多个环境? 最佳答案 同意@Ernest;它应该工作。这是一个示例...示例rake任务以回显变量:task:echo_envdoputs"VAR1:#{ENV['VAR1']}"puts"VAR2:#{ENV['VAR2']}"end执行任务:VAR1=fir