草庐IT

if-condition

全部标签

ruby - "if (a == b || c == b)"语句可以在 Ruby 中完成更短吗

我有一段Ruby代码如下:defcheckifa==b||c==b#executesomecode#b=thesamevariableendend可以这么写吗defcheckifa||c==b#thisdoesn'tdothetrickendif(a||c)==b#thisalsodoesn'tdothemagicasIthoughtitwouldendend或者以一种不需要输入两次b的方式。这是出于懒惰,我想知道。 最佳答案 if[a,c].include?b#codeend然而,这比您想避免的代码要慢得多——至少与a、b和c一样

ruby - "Assignment Branch Condition size for index is too high"是如何工作的?

Rubocop总是报告错误:app/controllers/account_controller.rb:5:3:C:AssignmentBranchConditionsizeforindexistoohigh.[30.95/24]ifparams[:role]@users=@search.result.where(:role=>params[:role])elsifparams[:q]&¶ms[:q][:s].include?('count')@users=@search.result.order(params[:q][:s])else@users=@search.result

ruby-on-rails - 添加内联类 if ...(在 1 行中)

我想将一个类添加到一个link_to只有当一个陈述为真时。"last"ifproduct==@products.last%>IF语句影响整行而不仅仅是:class部分的问题。我知道我可以用IFELSE完成它,但是是否可以在一行中完成它? 最佳答案 (product==@products.last?"last":"")%> 关于ruby-on-rails-添加内联类if...(在1行中),我们在StackOverflow上找到一个类似的问题: https://s

ruby - 硬件不可能? : "Create a rock paper scissors program in ruby WITHOUT using conditionals"

我正在上介绍性软件开发课,我的作业是创建一个带有两个参数的剪刀石头布程序(石头,纸)等,并返回获胜的arg。现在,如果我可以使用条件语句,我会快速解决这个问题,但作业说我们需要知道的一切都在前三个ruby教科书的章节,这些章节不包括条件!没有它们是否可以创建这个程序?或者他只是希望我们足智多谋并使用条件句?这是一个非常简单的条件分配......我在想我可能在这里遗漏了一些东西。编辑:我正在考虑那个chmod数字系统,并认为通过该加法系统可能有解决方案...... 最佳答案 这是一个只使用哈希的方法:RULES={:rock=>{:r

ruby-on-rails - Ruby on Rails : How to validate nested attributes on certain condition?

我有这些模型:classOrganisation:addressable,:dependent=>:destroyaccepts_nested_attributes_for:address,:allow_destroy=>trueendclassPerson:addressable,:dependent=>:destroyaccepts_nested_attributes_for:address,:allow_destroy=>true#Thesetwomethodsseemtohavenoeffectatall!validates_presence_of:organisation,:

ruby-on-rails - Ruby:IF 语句中的 Nils

这个问题在这里已经有了答案:HowtoavoidNoMethodErrorformissingelementsinnestedhashes,withoutrepeatednilchecks?(16个答案)关闭7年前。我正在处理的Rails应用程序中有以下非常难看的ruby​​代码:ifparams.present?ifparams[:search].present?ifparams[:search][:tags_name_in].present?...endendend我想问的是params[:search][:tags_name_in]是否已经定义,但是因为params、params

ruby - HAML - if/elsif 结构

我的HAML代码中需要这种构造:-ifsomething1%div.a-elsifsomething2%div.b-elsifsomething3%div.c-else%div.d%divanothercontent我希望我得到类似的东西:anothercontent但事实上我得到了anothercontent如果需要,我必须如何更新我的代码:另一个内容? 最佳答案 我认为您应该创建一个辅助方法:%div{:class=>helper_method(useful_parameters)}实现此目的的真正难看的方法是使用三元运算符(c

ruby - 为什么我不能在 ruby​​ 中做 "if zero"?

>>current_user.first_visit=>0>>ifcurrent_user.first_visit>>puts"test">>endtest=>nil为什么打印测试? 最佳答案 在Ruby中只有nil可以被认为是false的类比。没有假设0、""或[]表示false,因此您必须使用==0、.blank?、.empty?、.zero?等但是nil并不总是表现为false。例如,在字符串插值中,.to_s方法应用于#{}内容,它对FalseClass和NilClass的作用不同:irb(main)>"qwe#{false

ruby-on-rails - ruby rails : How to check if the Model Exists

我想知道如何检查模型是否已经存在于项目中?当用户尝试使用相同的模型名称以编程方式创建模型时,需要检查它是否已经存在? 最佳答案 定义了吗?如果模型已定义,ModelName将返回“常量”。 关于ruby-on-rails-rubyrails:HowtocheckiftheModelExists,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/11793869/

ruby-on-rails - 帮助重构这个讨厌的 Ruby if/else 语句

所以我有这个大而多毛的if/else语句。我将一个跟踪号传递给它,然后它确定它是什么类型的跟踪号。我怎样才能简化这件事?特别想减少代码行数。ifnum_length是的,我知道。这很讨厌。 最佳答案 试试这个。我使用case和正则表达式重写了它。我还使用:symbols而不是"strings"作为返回值,但您可以将其改回。tracking_service=casenumberwhen/^.Z/then:upswhen/^Q/then:dhlwhen/^96.{20}$/then:fedexwhen/^[HK].{10}$/then: