ruby 在每个循环中获取下一个值

发布于 2024-12-09 02:56:55 字数 190 浏览 0 评论 0原文

我可以在每个循环中获取下一个值吗?

(1..5).each do |i|
    @store = i + (next value of i)
end

答案是..

1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 = 29

我还能得到下一个值吗?

Can I get the next value in an each loop?

(1..5).each do |i|
    @store = i + (next value of i)
end

where the answer would be..

1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 = 29

And also can I get the next of the next value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在风中等你 2024-12-16 02:56:55

早于 Ruby 1.8.7,Enumerable 模块有一个方法 each_cons< /a> 这几乎完全符合你的要求:

每个_cons(n) { ... } → nil
each_cons(n) → an_enumerator

为每个连续的数组迭代给定的块。元素。如果没有给出块,则返回一个枚举器。

例如:

(1..10).each_cons(3) { |a|帕}
# 下面输出
[1,2,3]
[2,3,4]
[3,4,5]
[4,5,6]
[5,6,7]
[6,7,8]
[7,8,9]
[8,9,10]

唯一的问题是它不重复最后一个元素。但这是微不足道的修复。具体来说,您想要

store = 0
range = 1..5

range.each_cons(2) do |i, next_value_of_i|
    store += i + next_value_of_i
end
store += range.end

p store # => 29

但您也可以这样做:

range = 1..5

result = range.each_cons(2).reduce(:+).reduce(:+) + range.end

p result # => 29

或者,您可能会发现以下内容更具可读性:

result = range.end + range.each_cons(2)
                          .reduce(:+)
                          .reduce(:+)

From as early as Ruby 1.8.7, the Enumerable module has had a method each_cons that does almost exactly what you want:

each_cons(n) { ... } → nil
each_cons(n) → an_enumerator

Iterates the given block for each array of consecutive <n> elements. If no block is given, returns an enumerator.

e.g.:

(1..10).each_cons(3) { |a| p a }
# outputs below
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
[6, 7, 8]
[7, 8, 9]
[8, 9, 10]

The only problem is that it doesn't repeat the last element. But that's trivial to fix. Specifically, you want

store = 0
range = 1..5

range.each_cons(2) do |i, next_value_of_i|
    store += i + next_value_of_i
end
store += range.end

p store # => 29

But you could also do this:

range = 1..5

result = range.each_cons(2).reduce(:+).reduce(:+) + range.end

p result # => 29

Alternatively, you may find the following to be more readable:

result = range.end + range.each_cons(2)
                          .reduce(:+)
                          .reduce(:+)
难以启齿的温柔 2024-12-16 02:56:55

像这样:

range = 1..5
store = 0

range.each_with_index do |value, i|
  next_value = range.to_a[i+1].nil? ? 0 : range.to_a[i+1]
  store += value + next_value
end    

p store # => 29

可能有更好的方法,但这是有效的。

您可以像这样获取下一个值的下一个:

range.to_a[i+2]

Like this:

range = 1..5
store = 0

range.each_with_index do |value, i|
  next_value = range.to_a[i+1].nil? ? 0 : range.to_a[i+1]
  store += value + next_value
end    

p store # => 29

There may be better ways, but this works.

You can get the next of the next value like this:

range.to_a[i+2]
孤城病女 2024-12-16 02:56:55

一种不使用索引的方法是 Enumerable#zip:

range = 11..15
store = 0 # This is horrible imperative programming
range.zip(range.to_a[1..-1], range.to_a[2..-1]) do |x, y, z|
  # nil.to_i equals 0
  store += [x, y, z].map(&:to_i).inject(:+)
end
store

One approach that wouldn't use indexes is Enumerable#zip:

range = 11..15
store = 0 # This is horrible imperative programming
range.zip(range.to_a[1..-1], range.to_a[2..-1]) do |x, y, z|
  # nil.to_i equals 0
  store += [x, y, z].map(&:to_i).inject(:+)
end
store
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文