似乎RubyMine IDE在發現負面條件語句時發出警告。我想知道爲什麼使用負面條件語句是不好的?這純粹是由於可讀性?Ruby中的否定條件語句
例如,在這個代碼:
class Complement
def self.of_dna dna_strand
dna_array = dna_strand.chars
dna_complement = ['']
dna_structure = ['C', 'G', 'T', 'A']
dna_array.each do |strand|
unless dna_structure.include? strand
return ''
end
case strand
when "C"
dna_complement << "G"
when "G"
dna_complement << "C"
when "T"
dna_complement << "A"
when "A"
dna_complement << "U"
end
end
dna_complement.join('')
end
end
我想知道在這種情況下unless dna_structure.include? strand
和if !(dna_strucutre.include?)
之間有什麼不同?
你可以寫你的'case'聲明'dna_complement <<情況下的鏈;當「C」然後「G」時;當「G」然後「C」時;當「T」接着「A」時;當「A」然後「U」時; end'。或者,您可以編寫'dna_complement << strand.tr(「CGTA」,「GCAU」)'。 –