English 中文(简体)
提及在鲁比书写真实和虚假条件的方式
原标题:Preferred way to write true AND false conditional in Ruby

如果这个问题在某个地方得到回答,但我不赞成把这个问题留给谷歌去做,而且我没有看到任何风格的指南。

由于Ruby有多种方式在条件上显示消极态度,因此,最好写一种条件,即核查其中一部分是真实的,一部分是假的? 例:

if array && !array.include?( Bob )
  #do stuff!

但也可以说:

if array
  #do stuff! unless array.include?( Bob )

if array && not array.include?( Bob )
  #do stuff

if !array.nil? && !array.include?( Bob )

a) 除非:

unless array.nil?
  #do stuff unless array.include?( Bob )

还有一些。 任何被认为最鲁比什的想法? 背后意见的来源?

问题回答

就有文件记载的准则而言,唯一可以认为的是谷歌指南,该指南是“登t useand;始终使用&&>>----->。

除此之外,这在一定程度上取决于具体情况。 如果情况是真实的,又是假的,那么,如果你有的话,那么我肯定会把他们放在一起,如果他们有<条码>和印章;以及 !:

if array && !array.include?( Bob )
  #do stuff!

On the other hand, you might have additonal code that gets executed if the first condition is true even if the second one is also true; in that case, the nested unless or if makes sense:

if array 
    do stuff! unless array.include?  Bob 
    do other stuff anyway
end

这并不改进可读性,但仍然令人感兴趣。

如果你使用铁路,你可以使用主动支持网站try

if array.try(:include?,  Bob )
  # Do stuff




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签