简体   繁体   English

Rubocop Lint / Void:在散列的无效上下文中使用的文字

[英]Rubocop Lint/Void: Literal used in void context in a hash

I have this code, and rubocop tell me "Rubocop Lint/Void: Literal used in void context". 我有这段代码,rubocop告诉我“ Rubocop Lint / Void:在无效上下文中使用文字”。 How can I modify this problem and fix this message. 如何修改此问题并解决此消息。 Thanks. 谢谢。

e.messages.map.each { |key, message| { field: key, message: message } }

I think the problem is with using map and each altogether. 我认为问题在于使用map以及each map Yes, you can use both but there is no need to use both here. 是的,您可以同时使用两者,但此处无需同时使用两者。 Either you should use: 您应该使用:

e.messages.each { |key, message| { field: key, message: message } }

Or you should use: 或者您应该使用:

e.messages.map { |key, message| { field: key, message: message } }

The basic difference is that each returns the original array whereas map returns the resultant array. 基本区别在于, each返回原始数组,而map返回结果数组。

Here is the difference between map & each explained very well. 这是map之间的区别each解释都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM