Ruby:解析条件数组元素的最佳方法
我正在执行 API 调用,这些调用将有条件地返回几个不同的元素。我的代码目前是:
if array['productId']
value = array['productId'][0]
end
我觉得有一种更简洁的方法可以做到这一点。一些红宝石魔法。
I'm doing API calls that will conditionally return a couple different elements. My code is currently:
if array['productId']
value = array['productId'][0]
end
I feel like there is a more succinct way of doing this. Some Ruby magic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
更好的方法:
但是, array['productId'][0] 不是 ruby 天然的。你的数组由什么组成?
A better way :
However, array['productId'][0] is not ruby natural. What does your array consist of ?
从 Ruby 2.3.0 开始,最短的方法是 Array#dig:
Since Ruby 2.3.0, the shortest way is
Array#dig
:http://ruby-doc.org/core-2.3.0_preview1/Array.html#method-i-dig
我不确定您是暂时使用
value
还是稍后实际使用它,以及当条件不满足时您想对value
做什么。如果你想为丢失的键返回nil
,那么可以工作。否则,SpyrosP的答案将是最好的。
I am not sure if you are using
value
just temporarily or actually using it later, and what you want to do withvalue
when the condition is not met. If you want to returnnil
for missing keys, thencan work. Otherwise, SpyrosP's answer will be the best.
这可能有点迂腐,但为了确保它在所有情况下都有效,您不应该检查“not-nil”,而应该检查它是可索引的;像这样的东西:
或者甚至更好:
否则如果
array['productId'] == 2
你的代码将会失败(另一方面,考虑到所使用的密钥,这似乎是合理的 - 我会去product_ids
代替)。This might be a bit pedantic, but to make sure it works in all circumstances, you should not check 'not-nil', but rather that it is indexable; something like this:
Or even better:
Otherwise your code will fail if
array['productId'] == 2
(which on the other hand seems reasonable, given the key used - I would have goneproduct_ids
instead).您可以使用三元:
You could use a ternary:
您的代码模式看起来不错;稍微短一点是可以的...
Your code pattern looks OK; it's possible to be slightly shorter...
使用Ick 的也许模式,简洁而明确:
Using the maybe pattern of Ick, terse and explicit:
虽然我认为你的代码很好(虽然我更喜欢 SpyrosP 的单行版本),但你有一些可能性:
Rails 有 Object#try,它可以让您执行
array['productId'].try(:[], 0)
或array['productId' ].try(:at, 0)
.有些人喜欢 andand gem,它定义了
Object#andand
并且是使用方式类似于array['productId'].andand[0]
。While I think your code is fine (although I'd prefer SpyrosP's one-line version), you have some possibilities:
Rails has Object#try, which would let you do either
array['productId'].try(:[], 0)
orarray['productId'].try(:at, 0)
.Some people like the andand gem, which defines
Object#andand
and is used likearray['productId'].andand[0]
.哈,我喜欢这里的所有选择。但由于我没看到我最常用的是什么,所以我会再添加一个!
value = array['productId'] && array['productId'].first
(与
[0]
相比,我更喜欢.first
,因为它读起来更好一点)这假设您将有一个数组在
array['productId']
中,这是正确的方法(而不是类型检查)。否则,这与您的原始代码之间最大的区别是,如果数组没有任何内容,则这会导致
value
被分配给它nil
,而您的原始结果value
未定义(这可能会导致错误,具体取决于您以后如何使用它)。希望有帮助!
Ha, I love all the options here. But since I didn't see what I use most, I'll add one more!
value = array['productId'] && array['productId'].first
(I prefer
.first
to[0]
because it reads a little better)This presumes you'll have an array in
array['productId']
, which is the right way to do it (rather than type-checking).Otherwise, the biggest diference between this and your original code, is that this results in
value
havingnil
assigned to it if the array doesn't have anything, whereas your original results invalue
not being defined (which may cause errors, depending on how you use it down the road).Hope that helps!