Problem with x.downto(y) {}

There’s a problem I’ve been fighting since last week. Check this piece
of code out and tell me what’s wrong:

snip

@curLen.downto(1) do |i|
@snake[i].x = @snake[i - 1].x
@snake[i].y = @snake[i - 1].y
end

snip

@curLen is an integer with the value 20 and @snake is an array
containing structures for defining coordinates. The snippet above is a
part in a snake-clone where the element 0 in array @snake is the head
which is feeded with new values each cycle. This piece of code
controls the flow of coordinates for each point of the snake.
Every time I run this code Ruby dies with no message at all.
Help, please!

-Johan Persson

I can’t see anything wrong with your code, and I doubt anybody else will
either. For somebody to find “the problem”, you’ll need to post enough code
that:

  • can be run
  • gives rise to the error you’re talking about

That code should be a minimum. So you need some rudimentary definitions for
@snake and @curlen.

Cheers,
Gavin

···

----- Original Message -----
From: “Johan Persson” tokikenshi@anime-genesis.com

There’s a problem I’ve been fighting since last week. Check this piece
of code out and tell me what’s wrong:

snip

@curLen.downto(1) do |i|
@snake[i].x = @snake[i - 1].x
@snake[i].y = @snake[i - 1].y
end

snip

@curLen is an integer with the value 20 and @snake is an array
containing structures for defining coordinates. The snippet above is a
part in a snake-clone where the element 0 in array @snake is the head
which is feeded with new values each cycle. This piece of code
controls the flow of coordinates for each point of the snake.
Every time I run this code Ruby dies with no message at all.
Help, please!

-Johan Persson

snip

@curLen.downto(1) do |i|
@snake[i].x = @snake[i - 1].x
@snake[i].y = @snake[i - 1].y
end

snip

@curLen is an integer with the value 20 and @snake is an array
containing structures for defining coordinates. The snippet above is a
part in a snake-clone where the element 0 in array @snake is the head
which is feeded with new values each cycle. This piece of code
controls the flow of coordinates for each point of the snake.

I can’t find problem too since the following works:

arr = [1, 2, 3, 4, 5]
arr.size.downto(1) {|i| arr[i] = arr[i-1]}
arr[0] = 0
p arr  #=> [0, 1, 2, 3, 4, 5]

Every time I run this code Ruby dies with no message at all.
Help, please!

So really wanting to fix the problem, more information is needed.
However this particular case, I can propose an alternative: Use
Array#unshift.

arr = [1, 2, 3, 4, 5]
arr.unshift(0)
p arr  #=> [0, 1, 2, 3, 4, 5]

Urgh you don’t want the last element, you can kill it by Array#pop.
Maybe overkilling and inefficient, but it works.

···

In message ea5341b.0209231256.1dec46de@posting.google.com tokikenshi@anime-genesis.com writes:


kjana@dm4lab.to September 24, 2002
Art is long, and time is fleeting.

I’ve tried to provide as much relevant details as possible, and that’s
all I can scratch together.

However, thanks.

-Johan