Converting Operators From Strings Back Into a Symbol To Be Used In a Calculation
Published: 2020-03-07 23:24:45

\/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ 
If you are just here for the solution then scroll to the bottom 
\/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/
These past few weeks at Flatiron School my cohort has been learning about restful routes. Before moving to Rails developing we started with Sinatra to get an idea of what was happening behind the scenes of Rails magic. While completing one of the labs I found myself deep in a rabbit hole of Ruby incompatibility with Sinatra routes. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3eio7q2js5oq6tt4rizn.png)

Displayed above is the final tests that I had to pass in order to complete the lab. The user should be able to type something like “localhost:9393/+/5/4”. The page will then display the result of ‘9’. The route is simply: get '/:operation/:number1/:number2'. Now, I thought this should be super easy to write logic for! Just “(:number1 + :operation + :number2)”. Then I got this error:  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b8uazky58nbrrf4vgjuo.png)
This really had me stumped for some time. I knew that a case statement would have been the most obvious way of coding this but I just knew that my was would work. So, after fiddling around with this for a few hours I resorted to the solution to see if I was at least on the right path. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zewpyubfct9nite4kzjj.png)
Sure enough the solution was using this case statement but I was still unsatisfied because I really believe my method would work. This looks too messy and bulky for what it is actually doing, which is just a very simple calculation. I also assumed that the user would pass operations not string values. 

After some fiddling on my own I discovered that these routes were being passed as strings. So, rather than getting a + operator I was getting a “+” as a string. Typically when this happens we could just use .to_s or .to_i, but there is no .to_symbol method. Ill save the blood, sweat, and tears it took me to figure it out but eventually this I what I was able to come up with. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0xk3n8ahumonpdwxdsw2.png)
Isn’t it beautiful? It is a little hard to read though so here it is spread out. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7m5zmqnsdw5drlihstu3.png)
So finally, here is the SOLUTION. First, the operation string needs to be stored into a hash as a key. Now, if we called @op in this case we would get back :+.  It is now a symbol again hooray! After that is accomplished we are able to utilize the wonderful .send method which allows us to use different datatypes together without getting a TypeError. Finally, the result just needs to be converted back into a string so that it prints on the browser. 

In conclusion this was a lot of work and brain power that went into something that is probably pretty case-specific or even somewhat trivial. Though I hope that this post aids in another devs conquest to convert a “+” as a string back into a symbol. One more time let's compare the solution with my code.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7ku799y5t9hw1wmpkib0.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zsd9hf5eanjf41v1v78m.png)
The solution took 15 lines to do what my code did in just two. Therefore, I think it's safe to say that my method is a little bit more efficient. More than anything it was very satisfying finding this little logic detour to arrive at the same desired destination. 

Thank you for reading!