Ruby 注入索引和括号 [英] Ruby inject with index and brackets
问题描述
我尝试清理我的代码.第一个版本使用 each_with_index
.在第二个版本中,我尝试使用 Enumerable.inject_with_index-construct
压缩代码,我发现了 这里.
I try to clean my Code. The first Version uses each_with_index
. In the second version I tried to compact the code with the Enumerable.inject_with_index-construct
, that I found here.
它现在可以工作了,但在我看来就像第一个代码一样晦涩难懂.更糟糕的是我不明白元素周围的括号,索引在
It works now, but seems to me as obscure as the first code. Add even worse I don't understand the brackets around element,index in
.. .inject(groups) do |group_container, (element,index)|
但它们是必要的
- 这些括号有什么用?
- 如何使代码清晰易读?
class Array
# splits as good as possible to groups of same size
# elements are sorted. I.e. low elements go to the first group,
# and high elements to the last group
#
# the default for number_of_groups is 4
# because the intended use case is
# splitting statistic data in 4 quartiles
#
# a = [1, 8, 7, 5, 4, 2, 3, 8]
# a.sorted_in_groups(3) # => [[1, 2, 3], [4, 5, 7], [8, 8]]
#
# b = [[7, 8, 9], [4, 5, 7], [2, 8]]
# b.sorted_in_groups(2) {|sub_ary| sub_ary.sum } # => [ [[2, 8], [4, 5, 7]], [[7, 8, 9]] ]
def sorted_in_groups(number_of_groups = 4)
groups = Array.new(number_of_groups) { Array.new }
return groups if size == 0
average_group_size = size.to_f / number_of_groups.to_f
sorted = block_given? ? self.sort_by {|element| yield(element)} : self.sort
sorted.each_with_index do |element, index|
group_number = (index.to_f / average_group_size).floor
groups[group_number] << element
end
groups
end
end
第二个版本 -- 带有注入"和索引
class Array
def sorted_in_groups(number_of_groups = 4)
groups = Array.new(number_of_groups) { Array.new }
return groups if size == 0
average_group_size = size.to_f / number_of_groups.to_f
sorted = block_given? ? self.sort_by {|element| yield(element)} : self.sort
sorted.each_with_index.inject(groups) do |group_container, (element,index)|
group_number = (index.to_f / average_group_size).floor
group_container[group_number] << element
group_container
end
end
end
推荐答案
这些括号有什么用?
What is the use of these brackets?
这是 ruby 的一个非常好的特性.我称之为解构数组赋值",但它可能也有一个正式名称.
It's a very nice feature of ruby. I call it "destructuring array assignment", but it probably has an official name too.
这是它的工作原理.假设你有一个数组
Here's how it works. Let's say you have an array
arr = [1, 2, 3]
然后将这个数组分配给一个名称列表,如下所示:
Then you assign this array to a list of names, like this:
a, b, c = arr
a # => 1
b # => 2
c # => 3
你看,数组被解构"成它的单个元素.现在,到 each_with_index
.如您所知,它就像一个常规的 each
,但也返回一个索引.inject
不关心所有这些,它接受输入元素并将它们按原样传递给它的块.如果输入元素是一个数组(来自 each_with_index
的元素/索引对),那么我们可以在块体中将其拆开
You see, the array was "destructured" into its individual elements. Now, to the each_with_index
. As you know, it's like a regular each
, but also returns an index. inject
doesn't care about all this, it takes input elements and passes them to its block as is. If input element is an array (elem/index pair from each_with_index
), then we can either take it apart in the block body
sorted.each_with_index.inject(groups) do |group_container, pair|
element, index = pair
# or
# element = pair[0]
# index = pair[1]
# rest of your code
end
或者在块签名中解构该数组.有必要使用括号来提示 ruby 这是一个需要拆分为多个的单个参数.
Or destructure that array right in the block signature. Parentheses there are necessary to give ruby a hint that this is a single parameter that needs to be split in several.
希望这会有所帮助.
这篇关于Ruby 注入索引和括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!