if params[:parent_type] == "Order"
parent_id = nil
else
parent_id = params[:parent_id]
end
Would a Ruby person laugh at me for writing it this way? It doesn't seem particularly concise like some Ruby code I've seen.
一个Ruby人会嘲笑我这样写吗?它看起来并不像我见过的一些Ruby代码那样简洁。
6 个解决方案
#1
9
That looks perfectly reasonable to me. You could move the assignment in front of the if ( parent_id = if params...
) or use the ternary, but I don't think the result would look better.
这看起来非常合理。您可以在if前面移动赋值(parent_id = if params ...)或使用三元组,但我不认为结果看起来会更好。
If parent_id is nil or undefined before that line you can simply write:
如果parent_id在该行之前为nil或undefined,则可以简单地写:
parent_id = params[:parent_id] unless params[:parent_type] == "Order"
#2
7
Nothing really wrong with it as-is, but can be made more concise:
它没有什么问题,但可以更简洁:
parent_id = (params[:parent_type] == "Order") ? nil : params[:parent_id]
Alternatively:
或者:
parent_id = if (params[:parent_type] == "Order")
nil
else
params[:parent_id]
end
#3
5
I think it's fine the way it is. I'm a Ruby person, and I wouldn't laugh at you for writing it that way. It's clear what the code does and there's no real code duplication, so I wouldn't worry about it.
我认为它的方式很好。我是一个红宝石的人,我不会因为这样写的而嘲笑你。很清楚代码的作用是什么,并且没有真正的代码重复,所以我不担心它。
#4
2
I like:
我喜欢:
parent_id = (params[:parent_type] == "Order" ? nil : params[:parent_id])
#5
1
One more variation:
还有一个变化:
parent_id = (params[:parent_type] == "Order") && params[:parent_id]
#6
0
We can use Ruby ternary operator. Something like it:
我们可以使用Ruby三元运算符。喜欢它的东西:
parent_id = params[:parent_type] == "Order" ? nil : params[:parent_id]
If parent_id
is set with nil
value, we can write an unless syntax:
如果parent_id设置为nil值,我们可以编写一个除非语法:
parent_id = params[:parent_id] unless params[:parent_type] == "Order"
Or write a simple Ruby if-else in a different way you did it:
或者以不同的方式编写一个简单的Ruby if-else:
parent_id = if (params[:parent_type] == "Order")
nil
else
params[:parent_id]
end
For more Idiomatic Ruby way to code, I found an awesome article about that.
对于更多惯用Ruby方式的代码,我发现了一篇很棒的文章。
#1
9
That looks perfectly reasonable to me. You could move the assignment in front of the if ( parent_id = if params...
) or use the ternary, but I don't think the result would look better.
这看起来非常合理。您可以在if前面移动赋值(parent_id = if params ...)或使用三元组,但我不认为结果看起来会更好。
If parent_id is nil or undefined before that line you can simply write:
如果parent_id在该行之前为nil或undefined,则可以简单地写:
parent_id = params[:parent_id] unless params[:parent_type] == "Order"
#2
7
Nothing really wrong with it as-is, but can be made more concise:
它没有什么问题,但可以更简洁:
parent_id = (params[:parent_type] == "Order") ? nil : params[:parent_id]
Alternatively:
或者:
parent_id = if (params[:parent_type] == "Order")
nil
else
params[:parent_id]
end
#3
5
I think it's fine the way it is. I'm a Ruby person, and I wouldn't laugh at you for writing it that way. It's clear what the code does and there's no real code duplication, so I wouldn't worry about it.
我认为它的方式很好。我是一个红宝石的人,我不会因为这样写的而嘲笑你。很清楚代码的作用是什么,并且没有真正的代码重复,所以我不担心它。
#4
2
I like:
我喜欢:
parent_id = (params[:parent_type] == "Order" ? nil : params[:parent_id])
#5
1
One more variation:
还有一个变化:
parent_id = (params[:parent_type] == "Order") && params[:parent_id]
#6
0
We can use Ruby ternary operator. Something like it:
我们可以使用Ruby三元运算符。喜欢它的东西:
parent_id = params[:parent_type] == "Order" ? nil : params[:parent_id]
If parent_id
is set with nil
value, we can write an unless syntax:
如果parent_id设置为nil值,我们可以编写一个除非语法:
parent_id = params[:parent_id] unless params[:parent_type] == "Order"
Or write a simple Ruby if-else in a different way you did it:
或者以不同的方式编写一个简单的Ruby if-else:
parent_id = if (params[:parent_type] == "Order")
nil
else
params[:parent_id]
end
For more Idiomatic Ruby way to code, I found an awesome article about that.
对于更多惯用Ruby方式的代码,我发现了一篇很棒的文章。