gpt4 book ai didi

ruby - 样式/条件分配 : Use the return of the conditional for variable assignment and comparison

转载 作者:行者123 更新时间:2023-12-03 23:26:39 29 4
gpt4 key购买 nike

下面是我的 Ruby on Rails 代码 -

inactive_list = [1,2,3,4,5]
raw_data = []

data = {
name: "Test",
full_name: "Test data"
}
if inactive_list.include? <<id of data>>
data[:active] = false
else
data[:active] = true
end

raw_data << data
对于 if...else 语句,我收到 Rubocop linting 错误。我尝试进行一些更改,但无法修复 linting Rubocop 错误。
C: Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.

最佳答案

代替

if inactive_list.include? <<id of data>>
data[:active] = false
else
data[:active] = true
end
你可以写
data[:active] = !inactive_list.include? <<id of data>>
或者当你使用 Rails 时,你可以使用 exclude?而不是 include? 的否定:
data[:active] = inactive_list.exclude? <<id of data>>
完整示例:
data = {
name: "Test",
full_name: "Test data",
active: inactive_list.exclude?(<<id of data>>)
}

关于ruby - 样式/条件分配 : Use the return of the conditional for variable assignment and comparison,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63341511/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com