Question

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.

Était-ce utile?

La solution

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

You can also do this with #zip like...

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

With the #zip technique, the result will always have the first array's dimension, with the second truncated or nil-padded as needed. This may or may not be TRT. Transpose will raise IndexError in that case.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top