DEV Community

Cover image for Converting Operators From Strings Back Into a Symbol To Be Used In a Calculation
Aaidenplays
Aaidenplays

Posted on

Converting Operators From Strings Back Into a Symbol To Be Used In a Calculation

\/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/
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

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
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
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
Isn’t it beautiful? It is a little hard to read though so here it is spread out.
Alt Text
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
Alt Text
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!

Top comments (0)