There are many ways of doing the same thing in Ruby. How to check if a number is between the two values?
You can check if value is in the range by use of following methods,
These methods can be implemented as follows,
Let us initialize three numbers as follows,
But, I would suggest using between? than member? or include?
The reason behind preferring between that other two is Performance,
Suppose, we measure their Benchmarking performance as follows,
Here, we just have called the block with between, member, include significant number(1000000) of times to check the performance of these methods.
You might see there is no major difference. But, if you want to know why such difference is seen then you may refer the source of between, member, include methods and understand why they behave differently.
Use between rather than using member or include just for the performance optimization as explained above.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox