草庐IT

compile-time-constant

全部标签

ruby - 使用 Ruby,我如何迭代一个 for 循环 n.times

我有一个基本的ruby​​循环forvideoinsite.postsvideo.some_parameterendfor我想运行此循环2或3次。这可能吗? 最佳答案 3.timesdo#doworkhereend检查http://www.tutorialspoint.com/ruby/ruby_loops.htm 关于ruby-使用Ruby,我如何迭代一个for循环n.times,我们在StackOverflow上找到一个类似的问题: https://sta

ruby - 如何将两周添加到 Time.now?

如何在Ruby中的当前Time.now中添加两周?我有一个使用DataMapper的小型Sinatra项目,在保存之前,我有一个字段填充了当前时间加上两周,但没有按需要工作。任何帮助是极大的赞赏!我收到以下错误:NoMethodErrorat/undefinedmethod`weeks'for2:Fixnum这是模型的代码:classJobincludeDataMapper::Resourceproperty:id,Serialproperty:position,Stringproperty:location,Stringproperty:email,Stringproperty:ph

ruby-on-rails - rails 验证 : :allow_nil and :inclusion both needed at the same time

通常“种类”字段应允许留空。但如果不为空,则该值应包含在['a','b']validates_inclusion_of:kind,:in=>['a','b'],:allow_nil=>true代码不起作用? 最佳答案 在Rails5中,您可以在包含block的外部或内部使用allow_blank:true:validates:kind,inclusion:{in:['a','b'],allow_blank:true}或validates:kind,inclusion:{in:['a','b']},allow_blank:true提示

ruby-on-rails - 警告 : toplevel constant referenced

我有四个模型(Document、Question、Question::Document和Answer)。在我的Answer模型中有validates:text,presence:{:unless=>Proc.new{|a|a.question.is_a?Question::Document}}这给了我警告警告:Question::Document引用的顶层常量文档如何防止出现此警告(不重命名我的类)? 最佳答案 您的文件夹/文件结构应如下所示:app/models/question/document.rbanswer.rbdocum

ruby - Coffeescript 中等效的 Ruby .times

以下最简洁的Coffeescript是什么:#ruby3.times{puts'hi'}?我能想到的最好的是:#coffeescriptfornin[1..3]console.log'hi' 最佳答案 console.log'hi'for[1..3]同时正确处理0:console.log'hi'for[1..n]ifn或者使用原型(prototype)魔法:Number::times=(fn)->dofnfor[1..@valueOf()]if@valueOf()return3.times->console.log'hi'请注意,不

ruby-on-rails - 包含模块时出现 "Uninitialized constant"错误

我正在尝试引用关联扩展,但它出错了:NameError(uninitializedconstantUser::ListerExtension):app/models/user.rb:2:in`'这是我的实现:app/models/user.rbclassUsertrue,:extend=>Listerlib/lister.rbmoduleListerExtensiondeflisterself.map(&:to_s).join(',')endend我正在使用Railsv3.1.3。 最佳答案 AndrewMarshall对自动加载设

ruby - 烦人的 "warning: already initialized constant"消息的解决方案

今天我偶然发现了一个与Ruby常量有关的棘手问题。在我们的团队中,有人创建了一个模块,该模块包含在多个模型中。在我们的(规范)测试中,此结果输出到警告消息中,例如:/home/ayrton/project/lib/life_cycle.rb:5:warning:alreadyinitializedconstantRESET解决这个问题的一种方法是,像这样声明常量:moduleLifeCycleunless(const_defined?(:RESET))RESET='reset'end#...end我还阅读了AvdiGrimm撰写的博客文章,其中提供了替代方案solution,我想知道您

ruby - Xcode - 配置 : error: no acceptable C compiler found in $PATH

从头开始重建Mac。安装xcode和rvm然后尝试安装ruby但他们都给我:Errorrunning'./configure--prefix=/Users/durrantm/.rvm/rubies/ruby-1.9.3-p125--enable-shared--disable-install-doc--with-libyaml--with-opt-dir=/Users/durrantm/.rvm/usr',pleaseread/Users/durrantm/.rvm/log/ruby-1.9.3-p125/configure.logTherehasbeenanerrorwhilerun

ruby-on-rails - 如何将分钟添加到 Time 对象

在Ruby中,我该怎么做Time.now+10.hours?是否有secs和mins的等效项?例如:Time.now+15.mins 最佳答案 Ruby(编程语言)没有10.hours,它是作为RubyonRails(网络框架)一部分的ActiveSupport。是的,它确实同时具有minutes和seconds方法。然而,Time#+(Time实例上的+方法)返回一个新的Time实例,它是future那么多秒。因此,无需任何RubyonRails糖分,您只需执行以下操作:irb>t=Time.now#=>2011-08-0322:

ruby - 如何伪造 Time.now?

为了在单元测试中测试时间敏感的方法,设置Time.now的最佳方法是什么? 最佳答案 我真的很喜欢Timecop图书馆。您可以以block形式进行时间扭曲(就像时间扭曲一样):Timecop.travel(6.days.ago)do@model=TimeSensitiveMode.newendassert@model.times_up!(是的,您可以嵌套block形式的时间旅行。)您还可以进行声明式时间旅行:classMyTest我有一些cucumberTimecop的助手here.他们让你做这样的事情:Givenitiscurre