草庐IT

method-i-strftime

全部标签

ruby - “运行失败跳转到方法定义”错误 : undefined method `current_line' for TextMate:Module

更新:我想通了。Ctrl-F仅在未选择我正在搜索的方法时有效。游标只需要在方法名中。我刚升级到TextMate2。当我选择一个方法并使用Ctrl+F转到它的定义时,我得到:>FailurerunningJumptoMethodDefinition这是痕迹:/Users/ilikepie/Library/ApplicationSupport/TextMate/Managed/Bundles/RubyonRails.tmbundle/Support/lib/rails/text_mate.rb:54:in`method_missing':undefinedmethod`current_li

ruby - 如何在 ruby​​ 中组合包含 method_missing 的模块

我有几个模块缺少扩展方法:moduleSaysHellodefrespond_to?(method)super.respond_to?(method)||!!(method.to_s=~/^hello/)enddefmethod_missing(method,*args,&block)if(method.to_s=~/^hello/)puts"Hello,#{method}"elsesuper.method_missing(method,*args,&block)endendendmoduleSaysGoodbyedefrespond_to?(method)super.respond_

ruby-on-rails - 如何捕获 "undefined method ` [ ]' for nil:NilClass"错误?

我通过omniauth从facebook获得了一个嵌套数组,想检查它是否为空?/nil?/exists?依赖行看起来像:unlessomniauth['extra']['raw_info']['location']['name'].nil?这应该检查数组的这一部分是否为空或存在。但总是抛出这个错误:undefinedmethod`[]'fornil:NilClass我检查数组有误吗?我试过用“has_key”“nil?”“空的?”“存在?”“空白?”但这些都行不通!请帮助我,非常感谢! 最佳答案 理想情况下,您应该检查每个嵌套级别以

ruby - 在 Ruby 1.8.7 中显示时间减少到毫秒

我试图让我的应用程序显示时间精确到毫秒(例如11:37:53.231),但是1.8.7中的strftime似乎没有选项(http://ruby-doc.org/core-1.8.7/Time.html#method-i-strftime)。在Ruby>=1.9.3中,有%3N选项(http://ruby-doc.org/core-1.9.3/Time.html#method-i-strftime),但它不在1.8.7的文档中,而且似乎也不起作用。这是我在Ruby1.8.7中得到的输出。cur_time=Time.now#=>MonJun2412:43:14+09002013cur_ti

ruby-on-rails - rails : link_to calls custom method in controller

我希望使用link_to来调用我的Controller中的方法。但是,由于某些奇怪的原因,路由会寻找show方法。在我看来:..beverage.id)%>..在我的config/routes.rb中match'beverages/archive'=>'beverages#archive'在我的beverages_controller.rb中defarchivebeverage=Beverage.find(params[:id])respond_todo|format|#format.html#show.html.erbformat.json{renderjson:beverage}e

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-on-rails - Rails 上的 ruby : How to have multiple submit buttons going to different methods (maybe with with_action? )

这个问题在这里已经有了答案:HowdoIcreatemultiplesubmitbuttonsforthesameforminRails?(7个答案)关闭9年前。所以..'save'%>'library'%>然后在我的Controller中:with_actiondo|a|a.savedoenda.librarydoendend问题是只有一个操作被调用...两个submit_tags调用相同的操作...知道为什么吗?或者我如何获得两个按钮以将表单提交给两种不同的方法?

ruby - 为什么 `method=` 没有像其他任何方法一样对待?

考虑以下代码片段:classExampledefmy_attr=(value)@_my_attr=value@_my_attr*3endend我希望表达式Example.new.my_attr=5返回15,但事实证明这是错误的。始终返回原始返回值,即使我显式调用=方法也是如此:Example.new.my_attr=5#=>5Example.new.my_attr=(5)#=>5Ruby如何以及为何这样做?Ruby是否特别处理以=结尾的方法,还是采用其他机制?我想这排除了链接=方法的返回值,对吧?有没有办法让Ruby的行为有所不同,或者这就是它的样子?更新:感谢@jeffgran:Ex

ruby - strftime 数小时内无填充

当使用strftime@tzformat="%F,%l:00%p"时:我只想在逗号和小时之间留一个空格。但是%l没有为10、11和12提供空间,而如果我输入“%l”我会为0-9提供两个空间(一个来自填充,另一个来self添加的空间)。月份有无填充选项。我几个小时都不一样。我错过了什么? 最佳答案 -修饰符删除填充。如果你使用%-l而不是%l它根本不会放一个空格,你可以手动添加一个空格。Time.now.strftime@tzformat="%F,%-l:00%p"#=>"2015-01-29,8:00PM"(Time.now+360

ruby - "class#method"在 ruby 中是什么意思?

这个问题在这里已经有了答案:关闭13年前。PossibleDuplicate:Whyaremethodsinrubydocumentationprecededbyapoundsign?EDIT:DuplicateofWhyaremethodsinrubydocumentationprecededbyapoundsign?你好,我正在尝试利用免费的ProgrammingRuby书在业余时间学习Ruby以获取乐趣。它大部分是相当直接的,但我一直看到像这样的符号KaraokeSong#to_s,这在本书的前几章中并没有真正解释。我知道它的意思是#但它是您可以在代码中使用的东西吗?或者只是ru