草庐IT

if-cases

全部标签

Ruby - 退出 IF block

在IFblock中,我需要检查某些条件是否为真,如果为真,则退出block。#somethinglikethisif1==1returnifsome_object&&some_object.propertyputs'hello'end我该怎么做? 最佳答案 你不能像那样跳出if。您可以做的是向其添加一个子句:if(cond1)unless(cond2)#...endend如果您遇到逻辑嵌套过多的问题,并且需要一种更好地展平它的方法,也许您想做的是事先计算一个变量,然后仅在需要时才深入研究:will_do_stuff=cond1wil

ruby-on-rails - rails : How to test if an attribute of a class object is required in the model policy?

简单地说,我有一个模型用户,其中包含名称、电子邮件和评论作为属性。validates_presence_of:namevalidates_presence_of:email所以“姓名”和“电子邮件”是必需的,但不是“评论”。my_user=User.new我想找到一种方法来测试像my_user.name.required?或User.name.required?类似的东西。我的目标是创建一个表单,并根据该项目是否设置为“validates_presence_of”,将特定类动态添加到表单项目span或td我试图搜索它,但没有找到任何相关信息。有没有简单的方法可以做到这一点?谢谢

ruby - 2 if语句中的条件

我正在尝试检测电子邮件地址是否不是两个域之一,但我在使用ruby​​语法时遇到了一些问题。我目前有这个:if(!email_address.end_with?("@domain1.com")or!email_address.end_with?("@domain2.com"))#DoSomethingend这是条件的正确语法吗? 最佳答案 这里不是or,而是逻辑&&(和),因为您正试图找到匹配两者的字符串。if(!email_address.end_with?("@domain1.com")&&!email_address.end_w

ruby - 在 case 语句中使用类对象

在case语句中使用类对象的最佳方式是什么?假设我有a,它是Class类的一个实例。我想将它与不同的类(class)相匹配。如果我这样做caseawhenStringthen...whenFixnumthen...end这不会给出预期的结果,因为即使a==String例如,a===String也是不正确的。执行此操作的巧妙方法是什么? 最佳答案 我不会使用to_s,因为"String".to_s会是"String",所以也许我会使用casewhena==Stringthen...whena==Fixnumthen...end或a=S

ruby-on-rails - rails 上的 ruby : replace camel case with space

我想将camelCase之类的驼峰式单词转换为CAMELCASE。我尝试了提到的方法here.@q=params[:promo].underscore.humanize.upcase但这给了我CAMELCASE而不是CAMELCASE使用相同的结果:@q=params[:promo].gsub(/[a-zA-Z](?=[A-Z])/,'\0').downcase编辑:url包含/camelCase但在使用params[:promo]时,不保留驼峰式并且@q是camelcase 最佳答案 »'camelCase'.underscore

ruby-on-rails - 在 Ruby if 条件下使用正则表达式

我正在尝试将正则表达式用作Ruby(1.9.2)if语句中的条件,但即使正则表达式的计算结果为nil,它也会一直返回trueif(params[:test]=~/foo/)return"match"elsereturn"nomatch"end即使Rails.logger.info(params[:test])显示测试设置为"bar",上面的代码也会返回“匹配” 最佳答案 ifparams[:test]=~/foo/#Successfulmatchelse#Matchattemptfailedend适合我。调试params[:test

ruby - 在 ruby​​ 中哪个更快 - 散列查找或带有 case 语句的函数?

在时间紧迫的脚本中,我们有几个地方可以将旧ID转换为字符串。目前,我们在函数内部使用case语句,如下所示:defget_nameidcaseidwhen1"onething"when3"otherthing"else"defaultthing"endend我正在考虑将其替换为哈希查找,如下所示:NAMES={1=>"onething",3=>"otherthing",}NAMES.default="defaultthing"感觉使用NAMES[id]应该比使用get_name(id)更快-但真的是这样吗? 最佳答案 首先,有几点。

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-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 Greed Koan - 如何改进我的 if/then 汤?

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