jjzjj

implementation-defined-behavior

全部标签

ruby-on-rails - 如何在 Rails 中修复/调试 'expected x.rb to define X.rb'

我已经看到这个问题出现在许多不同的情况下,并希望获得在StackOverflow上修复/调试它的最佳实践。今天早上我想到了一个现实世界的例子:expectedannouncement.rbtodefineAnnouncement该类(class)在生产控制台的开发、测试和中表现良好,但在生产Mongrel中失败了。这是类(class):classAnnouncement'audio/mp3',:storage=>:s3end我想在答案中解决的问题与其说是解决这个具体问题,不如说是如何正确调试以使Rails给你一个有意义的错误,如预期的那样x.rbtodefineX.rb'通常是红色的鲱

ruby - 如何在 initialize() 中使用 define_method

尝试在initialize中使用define_method但得到undefined_methoddefine_method。我做错了什么?classCdefinitialize(n)define_method("#{n}"){puts"somemethod#{n}"}endendC.new("abc")#=>NoMethodError:undefinedmethod`define_method'for# 最佳答案 我怀疑您正在寻找define_singleton_method:define_singleton_method(symb

ruby - 在 Ruby 的 define_method 中使用 yield

是否可以让yield关键字在给定define_method的block内工作?简单示例:classTestdefine_method:testdo|&b|putsb#=>#yieldendendTest.new.test{puts"Hi!"}此代码在Ruby1.8.7和1.9.0中都会产生以下错误:test.rb:4:in`test':noblockgiven(LocalJumpError)fromtest.rb:8奇怪的是bblock变量!=nil但block_given?返回false。不识别Proc对象的block是Ruby的故意行为吗?编辑:向Beerlington的回答致意:

ruby - 如何使用 define_method 指定方法默认参数?

define_method可用于定义方法:define_method(:m)do|a|end等同于:defm(a)end但是,以下使用define_method的等效形式是什么:defm(a=false)end请注意,我需要能够在不提供任何参数的情况下调用m()。 最佳答案 这实际上就像您在Ruby1.9中所期望的那样工作!define_method:mdo|a=false|end如果您需要1.8兼容性,但不一定需要闭包来定义您的方法,请考虑使用带有字符串参数的class_eval并定期调用def:class_eval否则请按照ph

ruby-on-rails - 测试 : how to focus on behavior instead of implementation without losing speed?

似乎有两种完全不同的测试方法,我想引用它们。问题是,这些意见是在5年前(2007年)提出的,我很感兴趣,从那以后发生了什么变化,我应该走哪条路。BrandonKeepers:Thetheoryisthattestsaresupposedtobeagnosticoftheimplementation.Thisleadstolessbrittletestsandactuallyteststheoutcome(orbehavior).WithRSpec,Ifeellikethecommonapproachofcompletelymockingyourmodelstotestyourcontr

ruby-on-rails - ruby rails : How do you explicitly define plural names and singular names in Rails?

例如,我使用“Bonus”作为我的模型,所以我希望“bonuses”是复数形式而“bonus”是单数形式。但是,在Ruby中,这会导致:"bonus".pluralize#bonus"bonuses".singularize#bonuse因此,例如,当我执行“has_many:bonuses”时,它不会使用Bonus.rb模型(因为Ruby需要Bonuse.rb模型)。有没有一种方法可以在RubyonRails中以某种方式更正这一点,使“bonuses”充当模型bonus.rb的复数形式? 最佳答案 在config/initiali

ruby - 如何向 "not implemented yet"发送信号?

在新gem的初始起草中,我需要将一些方法实现留空(将在下一个实现)因此,我想发出“尚未实现”异常的信号我想知道是否有特定于Ruby语言的最佳实践或标准约定来编写这种占位符/异常。即:类似于:UnsupportedOperationException在javaNotImplementedException在.Net框架(C#)中 最佳答案 你应该引发NotImplementedErrorraiseNotImplementedErrorruby-doc 关于ruby-如何向"notimple

Ruby:define_method 与 def

作为一项编程练习,我编写了一个Ruby片段,它创建了一个类,实例化了该类中的两个对象,猴子修补了一个对象,并依靠method_missing猴子修补了另一个对象。这是交易。这按预期工作:classMonkeydefchatterputs"Iamachatteringmonkey!"enddefmethod_missing(m)puts"No#{m},soI'llmakeone..."defscreechputs"Thisisthenewscreech."endendendm1=Monkey.newm2=Monkey.newm1.chatterm2.chatterdefm1.screec

ruby - 如何使用 define_method 创建类方法?

如果您尝试以元编程方式创建类方法,这将很有用:defself.create_methods(method_name)#Tocreateinstancemethods:define_methodmethod_namedo...end#Tocreateclassmethodsthatrefertotheargsoncreate_methods:???end我要遵循的答案... 最佳答案 我认为在Ruby1.9中你可以这样做:classAdefine_singleton_method:loudlydo|message|putsmessag

ruby - 如何将参数传递给 define_method?

我想将参数传递给使用define_method定义的方法,我该怎么做? 最佳答案 传递给define_method的block可以包含一些参数。这就是您定义的方法接受参数的方式。当你定义一个方法时,你实际上只是给这个block起个绰号,并在类中保留对它的引用。参数随block一起提供。所以:define_method(:say_hi){|other|puts"Hi,"+other} 关于ruby-如何将参数传递给define_method?,我们在StackOverflow上找到一个类似