2

I'm coding a Google sketchup plugin with Ruby, and I faced a little problem. I have an array containing descriptions of every point like:

desc_array = ["anna ", "anna 45", "anna689", "anna36", "anna 888", "anna ",...]

The array containing every point's coordinates is:

todraw_su = [
  [-16.23317, -16.530533, 99.276929],
  [-25.142825, -12.476601, 99.237414],
  [-32.716122, -5.92341, 99.187951],
  [-38.964589, 4.181119, 99.182358],
  [-41.351064, 18.350418, 99.453714],
  [-40.797511, 33.987519, 99.697253],
  ...
]

I want to add a text in Google sketchup for each point. According to the Sketchup API this can be done by:

Sketchup.active_model.entities.add_text "This is the text", [x, y, z]

I tried:

todraw_su.each {|todraw| desc_array.each {|desc| Sketchup.active_model.entities.add_text desc,todraw }}

But, it gave me something unexpected, as it returns all the elements in desc_array for every element in to_draw.

What I want is every element in desc_array for every element in to_draw.

4

1 回答 1

4
[desc_array, todraw_su].transpose.each do |desc, coord|
  # ...
end

你也可以这样做#zip...

desc_array.zip(todraw_su).each do |desc, coord|
  # ...
end

使用#zip技术,结果将始终具有第一个数组的维度,并根据需要将第二个截断或填充为零。这可能是也可能不是 TRT。在这种情况下, Transpose 会引发IndexError

于 2013-02-07T15:42:01.340 回答