Access array data - problem

hi!

I’m having a problem with my array:

UI.messagebox ("layers: " + YV_layer_array.to_s)
=>output will be: “layers: 108, 190, 95”
(which is what I expected)

But when I try to access the individual items it seems to fail
UI.messagebox ("layer: " + YV_layer_array[0].to_s)
=>output will be: “layer: 57”
(which is wrong. It should be 108. I have no idea where 57 comes from)

Can anyone tell me how to access each items individually?

regards Rasmus

Am 19.06.2012 20:10, schrieb Rasmus S.:

I’m having a problem with my array:

UI.messagebox ("layers: " + YV_layer_array.to_s)
=>output will be: “layers: 108, 190, 95”
(which is what I expected)

But when I try to access the individual items it seems to fail
UI.messagebox ("layer: " + YV_layer_array[0].to_s)
=>output will be: “layer: 57”
(which is wrong. It should be 108. I have no idea where 57 comes from)

Hi Rasmus,

I am not able to reproduce your output:

irb(main):001:0> YV_layer_array = [108, 190, 95]
=> [108, 190, 95]
irb(main):002:0> 'layers: ’ + YV_layer_array.to_s
=> “layers: [108, 190, 95]”
irb(main):003:0> YV_layer_array[0].to_s
=> “108”
irb(main):004:0> YV_layer_array.class
=> Array

How is your YV_layer_array defined?

Regards,
Marcus

Rasmus S.:

But when I try to access the individual items it seems to fail
UI.messagebox ("layer: " + YV_layer_array[0].to_s)
=>output will be: “layer: 57”
(which is wrong. It should be 108. I have no idea where 57 comes from)

Could thin in any case be the character code of “9”? Ruby 1.8.7 behaves
like that:

“9”[0]
57

  • [murphy]

Maybe Marcus and Murphy…

Im using Ruby with Sketchup (Googles free drawing program).

In Sketchup its possible to make a webdialog which use html/JS which
communicate with Ruby via a callback function.

In the webdialog I define an array in html/Javascript like this:

var layer_array = [108,190,95];

in Javascript it works fine with:
Alert("layernum: " + layer_array[0]);
=> output is: (“layernum: 108”)

When I send it back to ruby and try:
UI.messagebox ("layernum: " + layer_array);
=> output is: (“layernum: 108,190,95”)

But if try this:
UI.messagebox ("layernum: " + layer_array[0]);
=> output is: (“layernum: 57”)

Maybe its because it sees it as a plain string as Murpy suggest.

It has to be said, if I define an array in Ruby, everythings work fine.
It seems to go wrong when send from JS/html to Ruby.

Any ideas how to work around this problem?

-Rasmus

On 20/06/2012, at 7:31 AM, Rasmus S. [email protected] wrote:

What does this show you?

UI.messagebox ("layernum: " + layer_array.class)

Henry

On 20/06/2012, at 8:28 AM, Rasmus S. wrote:

Henry, it doesnt change anything

What do you mean? It still outputs =>output will be: “layer: 57” That
would be very strange.

My point was, print out the class of the object so that you can work out
where it came from and what to do with it to get the answer you need.

Henry

Henry, it doesnt change anything :confused: tnx anyway

Am 19.06.2012 22:28, schrieb Rasmus S.:

Henry, it doesnt change anything :confused: tnx anyway

I think you first need to figure out what kind of object your
“array” is (therefore the suggested #class method). Most certainly
it is not a normal Array object, because Array#to_s does not join
the elements with commas.

Regards,
Marcus