title: Ruby and pointers
body: <p>I'm programming a Dungeon Generator for a litte game.</p>
  
  <p>Dungeons are made of rooms. A <code>room</code> has <code>connections</code> to other rooms.</p>
  
  <p><code>room.connections = [room_a, room_b]</code><br>
  and<br>
  <code>room.number = 1 # unique id</code></p>
  
  <p>Now I need to pick a room by it's number.</p>
  
  <p>I did this first with a <code>recursive_scan</code> method, which did not work because rooms can lead into circles, which throws a StackOverflowError. So I put an array called <code>already_scanned</code> with the room numbers, which were already picked into the args of the method. Then it didn't scan all rooms - btw, I have no idea why, by my logical undstandness it should have worked.</p>
  
  <p>Then I tried to put all rooms also in an array and then iterate the array for the wanted room - but here I get the problem, every room is basically connected to every other room, at least with some other rooms betwenn it; so the array gets as big as <code>dungeon_size * array_of_rooms.length</code>.</p>
  
  <p>What I need now is an explicit pointer - I know almost every var in ruby is a pointer, except Fixnums and Float (and maybe some other). Even though, the array gets to big, so I need a real pointer.</p>
  
  <p>(I also tried to setup an array of object_ids and load them via ObectSpace, but sadly - because I often have to load the rooms - the rooms with the wanted object_id are already recycled then, as an error message explains.)</p>
  
  <p>This is my recursive scan method:</p>
  
  <pre><code>def room(number)
    recursive_scan(@map, number, []) # @map is the entrance room
  end
  
  private
  
  def recursive_scan(room, number, scanned)
    scanned &lt;&lt; room.room_number
    if room.room_number == number
      room
    else
      r = nil
      room.connections.each do |next_room|
        if !scanned.include?(next_room.room_number)
          r = recursive_scan(next_room, number, scanned)
        end
      end
      r
    end
  end
  </code></pre>