Variable value error

7 views
Skip to first unread message

flesk

unread,
Jan 9, 2010, 7:55:42 PM1/9/10
to Google SketchUp Developers - SketchUp Ruby API
I'm trying to draw a simple Rubik cube with only 2 methods, drawCube
and drawRubik. I'm going to paste only the drawRubik's methods because
there is the trouble.

The problem is that the varible 'origen' is assigned at the start as
[0, 0, 0], and it isn't assigned anymore along the method but when the
method ends it returns a completly different value than the assigned.
I'm very angry about this because it's just stupid and it taked me 30
minutes!

Anyone knows why this happens?

def dibujarRubik(longitud, cubos)
origen = [0, 0, 0]
origenContador = origen
cubos.times do #CUBOS EN Z
cubos.times do # Cubos en Y
cubos.times do # Cubos en X
dibujarCubo(longitud, origenContador)
origenContador.x += longitud
end
origenContador.x = origen.x
origenContador.y += longitud
end
origenContador.y = origen.y
origenContador.z += longitud
end
origen
end

Todd Burch

unread,
Jan 9, 2010, 10:56:09 PM1/9/10
to Google SketchUp Developers - SketchUp Ruby API
Try changing these two lines:

origen = ORIGIN
origenContador = ORIGIN

That should fix it. Todd

Dan Rathbun

unread,
Jan 10, 2010, 2:12:21 AM1/10/10
to Google SketchUp Developers - SketchUp Ruby API
On Jan 9, 10:56 pm, Todd Burch wrote:
> Try changing these two lines:
>
> origen = ORIGIN
> origenContador = ORIGIN
>
> That should fix it.    Todd

NO that will not work. Because the '=' is creating an alias instead of
a new variable. Be careful! The constant 'ORIGIN' is frozen, and
frozen variables are not wanted.
(NO que no funcionará. Debido a que el '=' es crear un alias en lugar
de una nueva variable. ¡Ten cuidado! La constante 'ORIGIN' es
congelado, y las variables congelados no son deseados.)

Make sure to create a new object, and only reference the constant
'ORIGIN'. Then duplicate the variable using '.dup'
(Asegúrese de crear un nuevo objeto, y sólo la referencia constante
'ORIGIN'. Luego duplica la variable usando '.dup')

origen = Array.new( ORIGIN.to_a )
origenContador = origen.dup


If you wish to use 'Point3d':
(Si desea utilizar 'Point3d'):

origen = Geom::Point3d.new( ORIGIN )
origenContador = origen.clone

*Geom::Point3d class has no 'dup' method.
(Clase Geom::Point3d no tiene «método 'dup'.)

TIG

unread,
Jan 10, 2010, 8:51:32 AM1/10/10
to Google SketchUp Developers - SketchUp Ruby API
origen = Geom::Point3d.new(ORIGIN)
new_point = origen.clone
### Now the new point 'new_point' is made with the same values as
'origen', BUT it is separate from it...
[### Ahora el nuevo punto 'new_point' se hace con los mismos valores
de 'origen', PERO está separado de el...]
Note that '.clone' is equivalent to '.dup' for a Point3d...

Dan Rathbun

unread,
Jan 10, 2010, 9:41:01 AM1/10/10
to Google SketchUp Developers - SketchUp Ruby API
On Jan 10, 8:51 am, TIG wrote:
> origen = Geom::Point3d.new(ORIGIN)
> new_point = origen.clone

I said that. Wasn't I clear enough?

> On Jan 10, 7:12 am, Dan Rathbun wrote:
>
> > If you wish to use 'Point3d':
> > (Si desea utilizar 'Point3d'):
>
> > origen = Geom::Point3d.new( ORIGIN )
> > origenContador = origen.clone

On Jan 10, 8:51 am, TIG wrote:
> Note that '.clone' is equivalent to '.dup' for a Point3d...

Agreed. But it should not be.
This is another error in the Sketchup API.

.clone copies the frozen state of the object
.dup is NOT supposed to copy the frozen state
Both are supposed to copy the tainted state.

Geom::Point3d.clone violates the rule, and does not copy the frozen
state.
I suppose the .clone methods for Vector3d and Transformation are also
incorrect. ??

flesk

unread,
Jan 10, 2010, 12:59:53 PM1/10/10
to Google SketchUp Developers - SketchUp Ruby API
Thanks to everyone, now it works well :D:D
Reply all
Reply to author
Forward
0 new messages