# Please Forward: Ruby Quiz Submission

**URL:** <https://rubytalk.org/t/please-forward-ruby-quiz-submission/42763>\
**Category:** ruby-talk\
**Created:** [5 December 2007 14:58 UTC](https://rubytalk.org/t/please-forward-ruby-quiz-submission/42763 "2007-12-05T14:58:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [5 December 2007 14:58 UTC](https://rubytalk.org/t/please-forward-ruby-quiz-submission/42763/1 "2007-12-05T14:58:17Z")

</div>

> From: "Vasil Vangelovski" \<vvangelovski@gmail.com\>  
> Date: December 5, 2007 7:55:12 AM CST  
> To: submission@rubyquiz.com  
> Subject: Please Forward: Ruby Quiz Submission
> 
> #!/usr/bin/env ruby  
> #  
> # Created by Vasil Vangelovski on 2007-12-05.  
> # Copyright (c) 2007. All rights reserved.  
> #  
> # Solution for ruby quiz 148  
> # Thanx for posting something that can be solved on coffee-breaks
> 
> class String
> 
> #checks if the string is an operator  
> def op?  
> &nbsp;&nbsp;&nbsp;return (self=='+')||(self=='-')||(self=='\*')||(self=='/');  
> end
> 
> #returns true only for strings that represent integer or decimal numbers  
> def number?  
> &nbsp;&nbsp;&nbsp;#there's a way to simplify this!!! (maybe later)  
> &nbsp;&nbsp;&nbsp;int = false  
> &nbsp;&nbsp;&nbsp;dec = false  
> &nbsp;&nbsp;&nbsp;int = (/[0-9]+/.match(self)[0] == self) if !/[0-9]+/.match(self).nil?  
> &nbsp;&nbsp;&nbsp;dec =(/[0-9]+[.]{0,1}[0-9]+/.match(self)[0] == self) if  
> !/[0-9]+[.]{0,1}[0-9]+/.match(self).nil?  
> &nbsp;&nbsp;&nbsp;return int||dec  
> end
> 
> end
> 
> postfix\_exp=ARGV[0]  
> tokens = postfix\_exp.split(' ')
> 
> #the postfix-\>infix algo goes like this  
> stack =   
> tokens.each {|token|  
> if token.number?  
> &nbsp;&nbsp;&nbsp;stack.push(token)  
> elsif token.op?  
> &nbsp;&nbsp;&nbsp;string\_top = stack.pop  
> &nbsp;&nbsp;&nbsp;string\_bottom = stack.pop  
> &nbsp;&nbsp;&nbsp;#simple logic regarding operator precedence  
> &nbsp;&nbsp;&nbsp;stack.push("(#{string\_bottom}#{token}#{string\_top})") if (token  
> =='+')||(token=='-')  
> &nbsp;&nbsp;&nbsp;stack.push("#{string\_bottom}#{token}#{string\_top}") if (token  
> =='\*')||(token=='/')  
> else  
> &nbsp;&nbsp;&nbsp;#if it's not a number nor operator it's no valid  
> &nbsp;&nbsp;&nbsp;puts "Invalid input!"  
> &nbsp;&nbsp;&nbsp;exit(1)  
> end  
> }
> 
> infix = stack.to\_s  
> #remove trailing and leading parenthesis if any  
> infix = infix[1..(infix.size-2)] if ((infix[0]==40)&&(infix[infix.size-1]==41))  
> puts infix

> **···**
>
> Begin forwarded message:
