Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ruby
input = ARGV 
 
class RPNCalculator
 
    def evaluate(expression)
        to_evaluate = []
        a = expression.split(" ")
        a.each do |x|
            if (x =~ /\d/).nil? #return the index of the non-digit
                to_count = to_evaluate.pop(2)
                to_evaluate << to_count[0].send(x, to_count[1])
            else
                to_evaluate << x.to_i
            end
        end
        to_evaluate[0]
    end
end
 
 
calc = RPNCalculator.new
p calc.evaluate(input)
Posted
Updated 13-May-15 2:36am
v2

1 solution

That expression evaluates to true if the char is not a digit.

x =~ /\d/ runs the regular expression[^] \d on the char: if there is a digit in the string (which is, in this case, only 1 char), this expression returns the index of the digit. If it does not contain a digit, it returns nil.

.nil? checks whether x =~ /\d/ equals nil. If it does, it returns true. If it does not, it returns false.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900