Blackjack Game in Ruby I built a simple Blackjack game in Ruby on Rails, and am looking for feedback, because I believe I may have overcomplicated things. This was a homework assignment, where I was told to make 1 be an Ace card.

$number_array = []

def pull_push_random()
    number = Random.rand(1..10)
    $number_array.push(number)
    $array_sum = $number_array.inject(0){|sum,number_array| sum + number_array }
end

pull_push_random()
pull_push_random()
puts "This is an array #{$number_array}"

puts "Would you like to draw another card (random number)? y/n"
answer = gets.chomp
if answer == 'y'
    pull_push_random()
    puts "This is the array: #{$number_array}"
    if $array_sum &lt; 16
        pull_push_random()
        puts "This is the array #{$number_array}"
    elsif $array_sum &gt; 21
        puts "Done"
    else
        puts "Invalid option"
    end
elsif answer == 'n'
    puts "Done"
else
    puts "You did not enter a valid option."
end
You didn't implement the game of Blackjack

number = Random.rand(1..10)


This does not bear a strong resemblance to drawing from a shuffled deck of playing cards. A value of 10 should occur four times more often than 9, even if Aces are only valued as a 1. I understand the desire to simplify, but this goes a bit far.

To model a dealer with a large (or "infinite") number of decks shuffled into the shoe, draw with replacement by using Random.rand(1..13) and then map all values above 9 to 10, to account for J Q K.

To model a single shuffled deck, or a small number of them, that we draw from without replacement, allocate an array of size at least 52, and use Fisher-Yates-Knuth.


  aces are valued as either 1 or 11 according to the player's choice.


This rule is pretty central to the decision of whether to hit or pass.
You really can't use simple + (addition) when summarizing a player's hand.
Minimally you need to model the number of Ace cards he holds and (separately) the sum of non-Ace card values, to properly determine if he is "bust" yet. Also, please don't make $array_sum global. And a better name for pull_push_random() would be deal().

There are several other house rules, such as betting options, that would add more complexity if implemented. I agree with your design decision to avoid such complexities. One can still capture the essence of Blackjack in a fairly short program, if a handful of important rules are implemented.