How to check if a value is included between two other values? How to check if a value is included between two other values? ruby ruby

How to check if a value is included between two other values?


Ruby also has between?:

if value.between?(lower, higher) 


There are many ways of doing the same things in Ruby.You can check if value is in the range by use of following methods,

14.between?(10,20) # true

(10..20).member?(14) # true

(10..20).include?(14) # true

But, I would suggest using between than member? or include?. You can find more about it here.


You can express a <= x <= b as (a..b).include? x and a <= x < b as (a...b).include? x.

>> (33.75..56.25).include? 33.9=> true>> (33.75..56.25).include? 56.25=> true>>>> (33.75..56.25).include? 56.55=> false

Unfortunately, there seems no such thing for a < x <= b, a < x < b, ..

UPDATE

You can accomplish using (-56.25...-33.75).include? -degree. But it's hard to read. So I recommend you to use 33.75 < degree and degree <= 56.25.