# 2 questions about my array/range

**URL:** <https://www.ruby-forum.com/t/2-questions-about-my-array-range/143213>\
**Category:** Ruby\
**Created:** [July 16, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/2-questions-about-my-array-range/143213 "2008-07-16T13:09:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kain\_nobel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@kain\_nobel](https://www.ruby-forum.com/u/kain_nobel)\
**Post date:** [July 16, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/2-questions-about-my-array-range/143213/1 "2008-07-16T13:09:35Z")

</div>

Okay, so I wrote a script that’ll allow the game player to jump with the  
touch of a button. This script, I wanted a method that’ll adjust the  
distance in which  
the game player jumps, based on the farthest passible tile. To define  
the method, I used a ‘for i’ statement with 0 to the max distance, for  
each passible ‘space’ he can jump, I push the value into an array named  
‘@spaces’, respectively.

` def jump_dist
    @spaces = []
    case @direction
    when 2 # Down
      for i in 0...Jump::Distance
        if passable?(self.x, self.y + i, 2)
          @spaces[i] = i
          return @spaces if i == Jump::Distance
        end
      end
    when 4 # Left
      for i in 0...Jump::Distance
        if passable?(self.x - i, self.y, 4)
          @spaces[i] = i
          return @spaces if i == Jump::Distance
        end
      end
    when 6 # Right
      for i in 0...Jump::Distance
        if passable?(self.x + i, self.y, 6)
          @spaces[i] = i
          return @spaces if i == Jump::Distance
        end
      end
    when 8 # Up
      for i in 0...Jump::Distance
        if passable?(self.x, self.y - i, 8)
          @spaces[i] = i
          return @spaces if i == Jump::Distance
        end
      end
    end
  end`

…Maybe I wrote that a little oddly, correct me if you know a better  
functional method.

Anyways, next I test it with ‘print(jump\_dist)’ whenever the player  
‘jumps’ and it returns 0…3 (3 is Jump::Distance, BTW)

So basically, instead of returning an array, it returns a Range.

How do I get this to return me an array of only passable ‘spaces’, and  
secondly how do I pull the highest value out of that array?

---

<div class="post-metadata">

**Author:** ![kain\_nobel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@kain\_nobel](https://www.ruby-forum.com/u/kain_nobel)\
**Post date:** [July 16, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/2-questions-about-my-array-range/143213/2 "2008-07-16T13:57:19Z")

</div>

Hi –

On Wed, 16 Jul 2008, Kain Nobel wrote:

> case @direction  
> @spaces[i] = i  
> when 8 # Up  
> functional method.  
> Well, here’s an untested rewrite that might save some space:

def jump\_dist  
spaces = []

```
 0.upto(Jump::Distance-1) do |i|
   pass = case @direction
          when DOWN then passable?(self.x, self.y + i, 2)
          when LEFT then passable?(self.x - i, self.y, 4)
          when RIGHT then passable?(self.x + i, self.y, 6)
          when UP then passable?(self.x, self.y - 1, 8)
          end
   if pass
     spaces[i] = i
   end
 end

 return spaces

```

end

(I’ve turned @spaces into spaces, which is better unless you really  
need it as an instance variable.)

> Anyways, next I test it with ‘print(jump\_dist)’ whenever the player  
> ‘jumps’ and it returns 0…3 (3 is Jump::Distance, BTW)
> 
> So basically, instead of returning an array, it returns a Range.
> 
> How do I get this to return me an array of only passable ‘spaces’, and  
> secondly how do I pull the highest value out of that array?

The reason it’s returning a range is that your range is exclusive. The  
range (0…Jump::Distance) doesn’t include Jump::Distance. Therefore,  
the return statements never get executed. So what’s returned, instead,  
is the value of the whole for statement, which is the range itself.

To get the last element of an array: array[-1].

David

---

<div class="post-metadata">

**Author:** ![kain\_nobel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@kain\_nobel](https://www.ruby-forum.com/u/kain_nobel)\
**Post date:** [July 17, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/2-questions-about-my-array-range/143213/3 "2008-07-17T12:11:53Z")

</div>

Cool, that works wonders! Thanks David! _Adds special thanks to David in  
credit_

I tried posting this in another forum and everybody wanted me to copy  
someone else’s script!
