草庐IT

Determine_if_a_string_is_numeric

全部标签

ruby-on-rails - Ruby 1.8.7(或 Rails 2.x)中的 String.force_encoding()

是否有在Ruby1.8.7(或Rails2.x)中使用String.force_encoding()的解决方案,以便它像在Ruby1.9中一样工作?我读了一些关于requireactive_support的内容,但这不起作用$>gem列表--本地|grep'rails\|activesupport'activesupport(3.0.3,2.3.8,2.3.5)rails(2.3.8,2.3.5)$>ruby-vruby1.8.7(2010-08-16patchlevel302)[i686-darwin10.4.0]$>rails-vRails2.3.8IRB:>require"rub

ruby - 大多数方法中的 "This is a stub, used for indexing"?

我正在研究cursesgem的curses.rb,我发现它无处不在:defattrset(attrs)#Thisisastub,usedforindexingend#bkgdset(ch)##Manipulatethebackgroundofthecurrentwindow#withcharacterInteger+ch+##seealsoCurses.bkgdsetdefbkgdset(ch)#Thisisastub,usedforindexingend#bkgd(ch)##Setthebackgroundofthecurrentwindow#andapplycharacterInt

ruby-on-rails - slim 的模板 : Is it possible to put two elements on the same line?

我经常想嵌套元素,比如下面的导航:ullia(href="#")linkNamelia(href="#")linkNamelia(href="#")linkName是否可以将li和a放在同一行?像li>a这样的语法会很好。 最佳答案 我相信你可以做这样的事情ulli:ahref="#"Link1li:ahref="#"Link2参见内嵌标签:http://rdoc.info/gems/slim/file/README.md#Inline_tags 关于ruby-on-rails-slim

ruby-on-rails - ruby rails : How would i stay on the same page if the post is not saved?

defcreate@addpost=Post.newparams[:data]if@addpost.saveflash[:notice]="Posthasbeensavedsuccessfully."redirect_toposts_pathelseflash[:notice]="Postcannotbesaved,pleaseenterinformation."endend如果帖子未保存,则会重定向到http://0.0.0.0:3000/posts,但我需要留在页面上,带有文本输入字段,以便用户可以输入数据。后模型classPosttruevalidates:content,:pr

ruby - 没有将 nil 隐式转换为 String 错误

我有一个ruby​​脚本,它将通过获取和合并来自另一个文件的值来创建两个文件。#Resourcesrequire'rubygems'require'csv'col_date=[]col_constant1=[]col_constant2=[]col_appYear=[]col_statsDesc=[]col_keyStats=[]col_weeklyTotal=[]weekly_total=[]fname="finalStats.csv"#variableforcapturefilefinalStatsFile=File.open(fname,"w")#writetocapturefi

ruby-on-rails - 修复 "ruby installation is missing psych"错误?

我正在运行Rubyruby​​1.9.3p125,但我不断收到此错误,这让我无法在Rails中进行任何类型的开发。Itseemsyourrubyinstallationismissingpsych(forYAMLoutput).Toeliminatethiswarning,pleaseinstalllibyamlandreinstallyourruby.我用Google搜索了一下,似乎尝试了所有方法,但似乎没有任何效果。我真的很想开始一些新项目,但似乎无法绕过这个障碍。非常感谢任何帮助! 最佳答案 我在使用rvm安装ruby​​1.

ruby - `dirname' : can't convert nil into String (TypeError)

我不确定这是否真的在cucumber中,但以防万一你们中的一些人知道如何解决这个问题。基本上,我的测试突然停止工作,当我检查时,它显示了这个错误:C:/Vendor/Ruby187/lib/ruby/gems/1.8/gems/cucumber-rails-1.0.0/lib/cucumber/rails3.rb:3in'dirname':can'tconvertnilintoString(TypeError)是的,我先翻了个白眼,然后诅咒Windows,但我在我的Mac上试了一下,我得到了同样的结果:/Users/eumir/.rvm/gems/ruby-1.8.7-p174@myg

ruby-on-rails - rails : validating a field is present only if another is present

我有一个模型,其中有两个字段在技术上可以为空。字段名称是:is_activated和:activated_at。:activated_at仅在:is_activated设置为true时才需要。如果:is_activated为false,则不需要存在。在Rails中将此验证直接设置到ActiveRecord中的合适方法是什么? 最佳答案 您可以使用Proc在:activated_at验证器上。validates:activated_at,:presence,if:Proc.new{|a|a.is_activated?}推荐阅读:htt

ruby - 当字符串包含数字时,为什么 Ruby 的 String#to_i 有时会返回 0?

我刚刚尝试使用Ruby,然后遇到了String#to_i。假设我有这段代码:var1='6sldasdhkjas'var2='aljdfldjlfjldsfjl6'为什么putsvar1.to_i输出6而putsvar2.to_i给出0? 最佳答案 to_i方法返回由字符串开头处的所有可解析数字组成的数字。您的第一个字符串以带有数字的a开头,因此to_i返回该字符串,第二个字符串不以数字开头,因此返回0。顺便说一句,空格被忽略,所以"123abc".to_i返回123. 关于ruby-当

Ruby Greed Koan - 如何改进我的 if/then 汤?

我正在努力学习RubyKoans以尝试学习Ruby,到目前为止一切顺利。我已经得到了贪婪的公案,在撰写本文时它是183。我有一个可行的解决方案,但我觉得我只是拼凑了一堆if/then逻辑,但我不是拥抱Ruby模式。在下面的代码中,有什么方法可以让我更全面地接受Ruby模式吗?(我的代码包含在“我的代码[BEGINS|ENDS]HERE”注释中。#Greedisadicegamewhereyourolluptofivedicetoaccumulate#points.Thefollowing"score"functionwillbeusedcalculatethe#scoreofasing