English 中文(简体)
四组阵列
原标题:ruby working on array elements in groups of four
  • 时间:2010-01-01 10:04:27
  •  标签:
  • ruby

在每一要素需要处理时,我都有一个垃圾箱:

threads = []
elemets.each do  |element|
    threads.push(Thread.new{process(element)}}
end
threads.each { |aThread|  aThread.join }

由于资源限制,如果在某一时间处理4个要素,文稿的工作方式是最佳的。

no I know I can dump the each loop and use a variable to count 4 elements and then wait but is there a cooler ruby way to do it ?

最佳回答

在4个小组中可以列举:

>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].each_slice(4) {|a| p a}
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]

因此,你可以尝试像这样的东西。

elements.each_slice(4) do | batch |
    batch.each do | element |
        threads.push(Thread.new{process(element)}}

    end
    (do stuff to check to see if the threads are done, otherwise wait )
end

也许不是你们所需要的,尽管——我自3月大庆起就坐了,我只睡了几个小时。

问题回答

如果我读了你的话,你想要在一定时间进行不超过4的校对处理。

象你一样,对我来说,只有4条透镜,它们都从一个共同的Quue(标准校正部分)读到,处理这些内容。

当问题空洞时,你可以结束read。

将阵列分为4个平等阵列,每个阵容都有第1/4号程序,假设每个要素同时处理。 如果有些人需要比其他人更长的时间,那么你的一些座右铭将提前结束。

在共同点空空之前,不要read忙,因此,我认为这是一个更加有效的解决办法。

这是一项基于你的守则的工作方案,旨在表明:

require  thread 

elements = [1,2,3,4,5,6,7,8,9,10]

def process(element)
    puts "working on #{element}"
    sleep rand * 10
end

queue = Queue.new
elements.each{|e| queue << e }

threads = []
4.times do
    threads << Thread.new do
      while (e = queue.pop(true) rescue nil)
        process(e)
      end
    end
end

threads.each {|t| t.join }

rails(而不是Ruby)中,可以使用更可读的形式in_groups_of

arr= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
arr.in_groups_of(4, false) {|a| p a}

结果:

[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11]

最后一行只有3个要素,因为我们在in_group_of中指明了作为第二论点的错误。 如果你想要 n或任何其他价值,你可以以这种价值取代假。

是的,但你需要做一些办法。 通常的做法是凌驾于<代码>。

class Array
  def / len
    a = []
    each_with_index do |x,i|
      a << [] if i % len == 0
      a.last << x
    end
    a
  end
end 

既然如此界定,你现在可以轻易做到:

foo = [1,2,3,4,5,6]
foo / 2
# Result is [[1,2], [3,4], [5,6]]

不清楚以下变量是否仅仅使用“可变数4个要素”或可视为冷却,而是使你在尺寸不超过4个要素的斜体内出现阵列:

x = (1..10).to_a
0.step(x.size - 1, 4) do |i|
    # Choose one
    p x.slice(i, 4)
    p x[i, 4]
end




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签