Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does this line of ruby do?

I stumbled upon this line of Ruby in our codebase:

value = value.to_i if value == value.to_i

and I can’t understand what it is supposed to do.

If the guard condition is false, it does nothing.
If it is true, it’s a noop.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What gives?

>Solution :

This line of code will cast any integer number to the Integer class, so it will replace 2.0 or BigDecimal(2) with 2. This might be useful if you have code later on which cares about the class of value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading