我遇到布尔预期行为的问题。我可以做出理想的结果,但我不确定为什么发生这种奇怪的行为。这会产生一个错误的结果:布尔行为奇怪,逻辑问题
def palindrome?(string)
rev = string.reverse
if rev == string
true
else
false
end
end
palindrome?("abc") == false # => true
palindrome?("abcba") == true # => true
palindrome?("z") == true # => true
,而这将产生正确的结果:
def palindrome?(string)
rev = string.reverse
if rev == string
true
end
end
palindrome?("abc") == false # => false
palindrome?("abcba") == true # => true
palindrome?("z") == true # => true
以下可能是相似的:
def nearby_az(string)
counter = 0
string = string.split("")
if string.count == 1
false
elsif (string.index("z") - string.index("a")) <= 3
true
else
false
end
end
nearby_az("a") == false # => true
nearby_az("z") == false # => true
'回文?( “ABC”)''返回FALSE','假== FALSE'是'真',是什么奇怪的行为???? – xdazz
我试过了,但输出没有检出:'回文?(“abc”)== false:true ' – Deterius