草庐IT

LD_BIND_NOW

全部标签

ruby - Procs的绑定(bind)

是否可以在另一个对象的上下文中执行proc?我知道通常你会执行proc.call(foo),然后block应该定义一个参数。我想知道我是否可以让“self”绑定(bind)到foo,这样就不需要block参数了。proc=Proc.new{self.hello}classFoodefhelloputs"Hello!"endendfoo=Foo.new#Howcanprocbeexecutedwithinthecontextoffoo#suchthatitoutputsthestring"Hello"?proc.call 最佳答案 f

ruby-on-rails - Time.zone.now.to_date 是否等同于 Date.today?

Time.zone.now.to_date是否等同于Date.today?另一种表达方式:Time.zone.now.to_date==Date.today是否始终为true?如果不是,在应用程序时区中获取对应于“现在”的日期对象的最佳方法是什么? 最佳答案 它们并不总是相同的。Time.zone.now.to_date将使用应用程序时区,而Date.today将使用服务器时区。因此,如果两者位于不同的日期,那么它们就会不同。来self的控制台的示例:ruby-1.9.2-p290:036>Time.zone="Sydney"=>"

ruby-on-rails - DateTime.now 还是 Time.now?

我应该在我的Rails应用程序中使用两者中的哪一个:DateTime.noworTime.now在应用程序中同时使用两者有什么坏处吗?在上述(now)示例中,两者之间是否存在任何差异?(在我当前的系统上,它们同时显示) 最佳答案 使用(Date)Time.current而不是(Date)Time.nowRails扩展了Time和DateTime对象,并包含current属性,用于检索Rails环境设置的时间(默认=UTC),而不是服务器时间(可以是任何东西)。这很关键-您应该始终以UTC时间工作,除非在时区之间转换以供用户输入或显示

ruby - 在 ruby​​ 中更改 block 内的上下文/绑定(bind)

我有一个Ruby中的DSL,它的工作方式如下:desc'listalltodos'command:listdo|c|c.desc'showtodosinlongform'c.switch:lc.actiondo|global,option,args|#somecodethat'snotrelevanttothisquestionendenddesc'makeanewtodo'command:newdo|c|#etc.end一位开发人员建议我增强我的DSL以不需要将c传递给commandblock,因此不需要c.全部里面的方法;据推测,他暗示我可以使以下代码工作相同:desc'lista

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 - UTC 时间.now

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:HowdoIConvertDateTime.nowtoUTCinRuby?如何以日期时间毫秒和UTC获取当前时间?前任。2012-03-22T18:48:40.873Z我试过了-Time.now.utc_offset.to_sTime.now.xs_datetime

ruby-on-rails - 错误信息: Make sure that `gem install pg -v ' 0. 18. 1'` 绑定(bind)成功

我对ruby有疑问。我尝试了很多,但对我没有任何用处。当我想启动railsserver时,我得到这个错误信息:Anerroroccurredwhileinstallingpg(0.18.1),andBundlercannotcontinue.Makesurethat"geminstallpg-v'0.18.1"succeedsbeforebundling.这是我已经尝试过的:sudoinstallgembundleinstallbundleinstall--pathvendor/cachegeminstallpg-v'0.18.1'当我尝试geminstallpg-v'0.18.1'时

ruby - 如何伪造 Time.now?

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

ruby-on-rails - Rails DateTime.now 没有时间

我需要使用DateTime.now来获取当前日期,并“剥离”时间。例如,这显示了我不想要的:DateTime.now=>Sat,19Nov201118:54:13UTC+00:00这显示了我做想要的:DateTime.now.some_operation=>2011-11-0600:00:00UTC 最佳答案 您可以使用以下之一:DateTime.current.midnightDateTime.current.beginning_of_dayDateTime.current.to_date

ruby - TCP 服务器错误 : Address already in use - bind(2)

几周前Jekyll对我来说工作正常,但现在突然出现以下错误:TCPServerError:Addressalreadyinuse-bind(2)INFOWEBrick::HTTPServer#start:pid=7300port=4000%lsof-i:4000即使端口上没有任何运行。以下是详细信息:%jekyll--versionJekyll0.11.2%wherejekyll/home/bhaarat/.rvm/gems/ruby-1.9.2-p290/bin/jekyll/usr/bin/jekyll%ruby--versionruby1.9.2p290(2011-07-09re