ruby并行作业,步骤问题
所以,我试图通过做一些项目欧拉问题来学习红宝石,我遇到了一些我无法解释的事情,以及逗号?运算符?位于两者的中间。我还没有找到好的文档,也许我只是没有像我应该的那样使用谷歌,但是好的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1:这称为并行分配。 Ruby 关心创建时间变量,而不是用不正确的值覆盖变量。所以这个例子:
等同于:
bur更紧凑,没有地方犯愚蠢的错误等等。
2: ruby核心库中有2个
step
方法。第一个是Numeric
类(每个数字),所以你可以这样写:它从 5 开始,从中取出第二个数字,达到 100 时停止。
第二个
step
中Ruby 用于Range
:它获取范围(有开始和结束)并遍历它,获取每个第二个元素。它是不同的,因为您可以传递它不一定是数字范围,并且它将从中获取每个第 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:
is the same as:
bur more compact, without place to make stupid mistake and so on.
2: There are 2
step
methods in ruby core library. First is forNumeric
class (every numbers), so you could write:and it starts at 5 and takes every second number from it, stops when reaches 100.
Second
step
in Ruby is forRange
: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
这是一个并行赋值。在您的示例中,Ruby 首先评估
nxt
和cur + nxt
。然后,它将结果分别分配给cur
和nxt
。代码中的
step
方法实际上是Numeric#step
(范围由(n..m)
构造)。Numeric
的step
方法使用调用它的数字作为起点进行迭代。参数分别是极限和步长。因此,上面的代码将使用i * i
调用该块,然后每次连续递增i
,直到达到max
。Ruby 文档的一个很好的起点是 ruby-doc.org 站点。
This a parallel assignment. In your example, Ruby first evaluates
nxt
andcur + nxt
. It then assigns the results tocur
andnxt
respectively.The
step
method in the code is actuallyNumeric#step
(ranges are constructed with(n..m)
). Thestep
method ofNumeric
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 withi * i
and then each successive increment ofi
untilmax
is reached.A good starting point for Ruby documentation is the ruby-doc.org site.