ruby并行作业,步骤问题

发布于 2024-08-24 05:29:42 字数 856 浏览 3 评论 0原文

所以,我试图通过做一些项目欧拉问题来学习红宝石,我遇到了一些我无法解释的事情,以及逗号?运算符?位于两者的中间。我还没有找到好的文档,也许我只是没有像我应该的那样使用谷歌,但是好的 ruby​​ 文档似乎有点稀疏。 。 。

1:您如何描述这是如何工作的?第一个片段是我不理解的 ruby​​ 代码,第二个片段是我编写的代码,只有在煞费苦心地跟踪第一个片段之后才执行相同的操作:

#what is this doing?
cur, nxt = nxt, cur + nxt

#this, apparently, but how to describe the above?
nxt = cur + nxt   
cur = nxt - cur   

2:在下面的示例中,您如何描述带有“step”的行正在做什么?据我所知,step命令的工作原理类似于(range).step(step_size),但这似乎是在做(starting_point).step(ending_point, step_size)。我的这个假设正确吗?我在哪里可以找到这方面的好文档?

#/usr/share/doc/ruby1.9.1-examples/examples/sieve.rb  
# sieve of Eratosthenes
max = Integer(ARGV.shift || 100)
sieve = []
for i in 2 .. max
  sieve[i] = i
end

for i in 2 .. Math.sqrt(max)
  next unless sieve[i]
  (i*i).step(max, i) do |j|
    sieve[j] = nil
  end
end
puts sieve.compact.join(", ")

so, i'm trying to learn ruby by doing some project euler questions, and i've run into a couple things i can't explain, and the comma ?operator? is in the middle of both. i haven't been able to find good documentation for this, maybe i'm just not using the google as I should, but good ruby documentation seems a little sparse . . .

1: how do you describe how this is working? the first snippet is the ruby code i don't understand, the second is the code i wrote that does the same thing only after painstakingly tracing the first:

#what is this doing?
cur, nxt = nxt, cur + nxt

#this, apparently, but how to describe the above?
nxt = cur + nxt   
cur = nxt - cur   

2: in the following example, how do you describe what the line with 'step' is doing? from what i can gather, the step command works like (range).step(step_size), but this seems to be doing (starting_point).step(ending_point, step_size). Am i right with this assumption? where do i find good doc of this?

#/usr/share/doc/ruby1.9.1-examples/examples/sieve.rb  
# sieve of Eratosthenes
max = Integer(ARGV.shift || 100)
sieve = []
for i in 2 .. max
  sieve[i] = i
end

for i in 2 .. Math.sqrt(max)
  next unless sieve[i]
  (i*i).step(max, i) do |j|
    sieve[j] = nil
  end
end
puts sieve.compact.join(", ")

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

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

发布评论

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

评论(2

本宫微胖 2024-08-31 05:29:42

1:这称为并行分配。 Ruby 关心创建时间变量,而不是用不正确的值覆盖变量。所以这个例子:

cur, nxt = nxt, cur + nxt

等同于:

tmp = cur + nxt
cur = nxt
nxt = tmp

bur更紧凑,没有地方犯愚蠢的错误等等。

2: ruby​​核心库中有2个step方法。第一个是 Numeric 类(每个数字),所以你可以这样写:

5.step(100, 2) {}

它从 5 开始,从中取出第二个数字,达到 100 时停止。

第二个 step 中Ruby 用于 Range

(5..100).step(2) {}

它获取范围(有开始和结束)并遍历它,获取每个第二个元素。它是不同的,因为您可以传递它不一定是数字范围,并且它将从中获取每个第 n 个元素。

看看 http://ruby-doc.org/core-1.8.7 /index.html

1: It's called parallel assignment. Ruby cares to create temporal variables and not override your variables with incorrect values. So this example:

cur, nxt = nxt, cur + nxt

is the same as:

tmp = cur + nxt
cur = nxt
nxt = tmp

bur more compact, without place to make stupid mistake and so on.

2: There are 2 step methods in ruby core library. First is for Numeric class (every numbers), so you could write:

5.step(100, 2) {}

and it starts at 5 and takes every second number from it, stops when reaches 100.

Second step in Ruby is for Range:

(5..100).step(2) {}

and it takes range (which has start and end) and iterates through it taking every second element. It is different, because you could pass it not necessarily numeric range and it will take every nth element from it.

Take a look at http://ruby-doc.org/core-1.8.7/index.html

2024-08-31 05:29:42
  1. 这是一个并行赋值。在您的示例中,Ruby 首先评估 nxtcur + nxt。然后,它将结果分别分配给 curnxt

  2. 代码中的step方法实际上是Numeric#step(范围由 (n..m) 构造)。 Numericstep 方法使用调用它的数字作为起点进行迭代。参数分别是极限和步长。因此,上面的代码将使用 i * i 调用该块,然后每次连续递增 i,直到达到 max

Ruby 文档的一个很好的起点是 ruby-doc.org 站点。

  1. This a parallel assignment. In your example, Ruby first evaluates nxt and cur + nxt. It then assigns the results to cur and nxt respectively.

  2. The step method in the code is actually Numeric#step (ranges are constructed with (n..m)). The step method of Numeric iterates using the number it is called on as a starting point. The arguments are the limit and step size respectively. The code above will therefore invoke the block with i * i and then each successive increment of i until max is reached.

A good starting point for Ruby documentation is the ruby-doc.org site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文