The update method of the EnemyFormation class handles the movement pattern of the classic Space Invaders game. It will check if any of the aliens have reached the edge of the screen, and if so change direction and drop down. This is done by setting up a special SCREEN_BOUNDS hash of rectangles.
Imagine the screen bounds rectangles as invisible rectangles just off the edge of the screen. If any of the enemies touch these rectangles, they've gone off the screen. If any of them have gone off the screen, the direction flag is flipped and the entire formation is moved down. This collision detection with the edge of the screen is done in the update method.
# Gone off the rigth side?
if @dir == 1 and e.rect.collide_rect?( SCREEN_BOUNDS[:right] )
@dir = -1
# Gone off the left side?
elsif @dir == -1 and e.rect.collide_rect?( SCREEN_BOUNDS[:left] )
@dir = 1
call :update, delta, @dir