未验证 提交 a3f4a62e 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #38677 from eugeneius/index_by_index_with_docs

Clarify index_by and index_with docs [ci skip]
......@@ -44,7 +44,8 @@ def sum(identity = nil, &block)
end
end
# Convert an enumerable to a hash keying it by the block return value.
# Convert an enumerable to a hash, using the block result as the key and the
# element as the value.
#
# people.index_by(&:login)
# # => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
......@@ -61,12 +62,19 @@ def index_by
end
end
# Convert an enumerable to a hash keying it with the enumerable items and with the values returned in the block.
# Convert an enumerable to a hash, using the element as the key and the block
# result as the value.
#
# post = Post.new(title: "hey there", body: "what's up?")
#
# %i( title body ).index_with { |attr_name| post.public_send(attr_name) }
# # => { title: "hey there", body: "what's up?" }
#
# If an argument is passed instead of a block, it will be used as the value
# for all elements:
#
# %i( created_at updated_at ).index_with(Time.now)
# # => { created_at: 2020-03-09 22:31:47, updated_at: 2020-03-09 22:31:47 }
def index_with(default = INDEX_WITH_DEFAULT)
if block_given?
result = {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册