title: Using too much stack memory in Ruby
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/12728186/stack-level-too-deep-in-ruby-trying-to-draw-a-random-card">Stack level too deep in Ruby trying to draw a random card</a>  </p>
  </blockquote>
  
  
  
  <p>This is a Blackjack simulator I made in order to check the percentage of times one can win given different strategies. The code:</p>
  
  <pre><code>one_suit = [2,3,4,5,6,7,8,9,10,10,10,10,11]; #the value of the cards for blackjack
  $full_deck = one_suit*4; #clubs, diamonds, hearts and spades
  $deck = $full_deck; #start off the game with a full deck
  
  class Player
    attr_accessor :ace_count
    attr_accessor :hand_value
  
    def initialize(ace_count,hand_value)
      @ace_count  = ace_count;
      @hand_value = hand_value;
    end
  
    def hit #instance method, vs. self.hit = class method
      choice_of_card = rand($deck.length); #choose a random card out of the deck
      drawn_card = $deck[choice_of_card]; #draw that random card from the deck
      if drawn_card != 0 #if there is a card there 
       $deck[choice_of_card] = 0; #remove that card from the deck by making the space blank
       if drawn_card == 11 #if you draw an ace
        self.ace_count += 1;
       end 
       self.hand_value += drawn_card ;
      else hit; #if there is no card at that space then redraw (recursion)
      end
    end
  
    def flip_aces
      while self.hand_value &gt; 21 &amp;&amp; ace_count &gt; 0 #repeat until hand is below 21 or aces are all flipped
       self.ace_count -= 1 #ace gets flipped to a 1
       self.hand_value -= 10 #ace goes from 11 to 1
      end
    end
  
  end
  
  
  def oneplayergame
   $deck = $full_deck; #start a new game with a full deck 
   #generate the house and the player 
   house = Player.new(0,0);
   player1 = Player.new(0,0);
   #both the house and the player draw two cards
   house.hit; house.hit; player1.hit; player1.hit;
   while player1.hand_value &lt;= 16 #PLAYER STRATEGY: ON WHAT CARD DOES THE PLAYER STAND
    player1.hit;
    if player1.hand_value &gt; 21
     player1.flip_aces; 
    end
   end
   while house.hand_value &lt;= player1.hand_value &amp;&amp; player1.hand_value &lt;=21 #HOUSE DRAWS CARDS IF UNDER PLAYER VALUE AND PLAYER IS NOT BUST
    house.hit;
    if house.hand_value &gt; 21
     house.flip_aces; 
    end
   end
   #outcome
   #puts player1.hand_value;
   #puts house.hand_value;
   if player1.hand_value &lt; house.hand_value
    return -1;
   elsif player1.hand_value &gt; house.hand_value
    return 1;
   else return 0;
   end
  end
  
  #the simulation
  wins = 0;
  losses = 0;
  rounds=5;
  
  for i in 1..rounds
   if oneplayergame &gt;0 
    wins +=1;
   elsif oneplayergame &lt;0
    losses +=1
   end
  end
  
  print wins/(wins+losses).round(3)*100;
  </code></pre>
  
  <p>However, trying to run it with more than 5 rounds, I run into the following error:</p>
  
  <blockquote>
    <p>Error: Your application used more stack memory than the safety cap of 2048K.</p>
  </blockquote>
  
  <p>I was wondering if someone could see where in the code I was using memory very inefficiently. Could it be the recursion? However, the cards are placed back in the deck at the start of each simulation.</p>