title: Ruby calculator - hash not storing correctly
body: <p>So I'll start off by writing that I am new to this site (today), as well as to the Ruby programming language (3 days ago), so don't feel afraid to rip apart my code--I am trying to learn and get better.</p>
  <p>Basically.. I am creating a console calculator that is able to read a simple math problem (or string of math problems) from the user and solve the equation. It doesn't use order of operations or anything fancy (yet) and it is basically working except for this one weird bug I can't figure out.</p>
  <pre><code>Userinput = &quot;1 + 2 + 3 - 4&quot;
  # First I split the user input into an array of stirngs and then loop over the
  # array of strings and depict whether a string is a key or hash (see code below)
  
  # program should store these characters in a hash like so.. 
  hash = { nil=&gt;1, &quot;+&quot;=&gt;2, &quot;+&quot;=&gt;3, &quot;-&quot;=&gt;4 }
  </code></pre>
  <p>Then I would use the key of the hash to determine whether or not I was adding, subtracting, multiplying, or dividing next.</p>
  <p>Everything pretty much works fine! Its just that when I do a problem with more than 2 operations (i.e. 1 + 2 - 0 + 3) the program will just randomly leave out some keys and operators. I have been trying different examples to search for a pattern but I cant find the source. Below I'll post examples of the problem and their output, as well as the hash itself, and then full source code. Thanks in advance for any help or critiques!</p>
  <p><em><strong>Examples format</strong></em></p>
  <p>Program Input (user prompt, user input) --
  Program output (sum of equation) --
  hash at the end of execution</p>
  <p>Example 1</p>
  <blockquote>
  <p>Type a math problem (ex. 40 / 5): 40 / 5 + 2 - 5 * 5 - 5 * 5 - 100</p>
  <p>-450</p>
  <p>{nil=&gt;40, &quot;/&quot;=&gt;5, &quot;+&quot;=&gt;2, &quot;-&quot;=&gt;100, &quot;*&quot;=&gt;5}</p>
  </blockquote>
  <p>Example 2</p>
  <blockquote>
  <p>Type a math problem (ex. 40 / 5): 1 + 2 - 0 + 3</p>
  <p>4</p>
  <p>{nil=&gt;1, &quot;+&quot;=&gt;3, &quot;-&quot;=&gt;0}</p>
  </blockquote>
  <p>Example 3</p>
  <blockquote>
  <p>Type a math problem (ex. 40 / 5): 10 - 5 * 2 + 8 + 2</p>
  <p>12</p>
  <p>{nil=&gt;10, &quot;-&quot;=&gt;5, &quot;*&quot;=&gt;2, &quot;+&quot;=&gt;2}</p>
  </blockquote>
  <p>Source code: main.rb</p>
  <pre><code>=begin
    
    main.rb
    Version 1.0
    Written by Alex Hail - 10/16/2016
   
    Parses a basic, user-entered arithmetic equation and solves it
   
  =end
  
  @operationsParser = &quot;&quot;    # global parser
  @lastKeyAdded = &quot;&quot;
  
  private
  def appointType(sv)
      if sv =~ /\d/
        sv.to_i
      else
        sv
      end
  end
  
  private
  def operate(operations)
    sum = 0
    operations.each do |k, v|
      if k.nil?
        sum += v
      else
        case k
          when '+' then sum += v
          when '-' then sum -= v
          when '*' then sum = sum * v  
          when '/' then sum = sum / v
          else
        end
       end
     end
     sum
   end
  
  private
  def solveEquation
    print &quot;Type a math problem (ex. 40 / 5): &quot;
    userInput = gets.chomp
    
    #array to hold all numbers and their cooresponding operation
    operations = {}   # &lt;== Empty hash
    
    #split the user input via spaces
    @operationsParser = userInput.split(&quot; &quot;)
    
    #convert numbers into numbers store operators in hash ( nil =&gt; 40, &quot;/&quot; =&gt; 5) -- would be 40 / 5
    @operationsParser.each do |stringValue|
      if appointType(stringValue).is_a? Integer
        
  
       operations[@lastKeyAdded != &quot;&quot; ? @lastKeyAdded : nil] = appointType(stringValue)
          
      else #appointType will return a string by default
        keyToAdd = appointType(stringValue)
        @lastKeyAdded = keyToAdd
      end
    end
    
    #check if operators(+, *, -, /, or nil) in the keys are valid, if not, error and exit, if so, operate
    operations.each do |k,v|
      case k
        when '+'
        when '-'
        when '*'    
        when '/'
        when nil
        else
          # Exit the program if we have an invalid operator in the hash
          puts &quot;Exiting program with error - Invalid operator used (Only +, -, *, / please)&quot;
          return
      end
    end
    
    sum = operate(operations)
    
    
    puts sum, operations
  end
  
  solveEquation
  </code></pre>