我如何扭转否定条件并在此红宝石语法中交换IF-ELSE分支
VS代码在这条线上显示一条线条,我想知道为什么。 这是代码行:
if answer != ""
它说:
扭转否定条件并交换if-else分支。
在代码片段下面找到下面
def coach_answer(your_message)
if your_message.downcase == "i am going to work right now!"
""
elsif your_message.end_with?("?")
"Silly question, get dressed and go to work!"
else
"I don't care, get dressed and go to work!"
end
end
def coach_answer_enhanced(your_message)
answer = coach_answer(your_message)
if answer != ""
if your_message.upcase == your_message
"I can feel your motivation! #{answer}"
else
answer
end
else
""
end
end
VS Code is showing a squiggly line on this line and I wonder why.
Here is the line of code:
if answer != ""
It says:
Invert the negated condition and swap the if-else branches.
Find below the code snippet
def coach_answer(your_message)
if your_message.downcase == "i am going to work right now!"
""
elsif your_message.end_with?("?")
"Silly question, get dressed and go to work!"
else
"I don't care, get dressed and go to work!"
end
end
def coach_answer_enhanced(your_message)
answer = coach_answer(your_message)
if answer != ""
if your_message.upcase == your_message
"I can feel your motivation! #{answer}"
else
answer
end
else
""
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
转换代码:
,这意味着您应该从以下方面
当Vs说“倒置否定条件并交换If-else分支” 成为
==
,因此,如果 branch “分支a” 和else
branch “分支” b“ 交换了。应用于您的代码,建议这样写:
When VS says "Invert the negated condition and swap the if-else branches" it means that you should convert your code from:
to:
The negated condition
!=
was inverted to become==
and accordingly, theif
branch "branch A" and theelse
branch "branch B" were swapped.Applied to your code, it suggests to write it this way: