Hi guys! I am just starting to have my hands on ruby. So I saw that
puts (2…6) === 5.3
outputs true, but number 5.3 is not being contained in the sequence. Sequence 2…6 contains numbers 2,3,4,5,6 right?
(2..6)
is a range, not a sequence. You can make it a sequence with (2..6).to_a
and rewrite the above as:
(2..6).to_a.include? 5.3
# => false
2 Likes
While this does the job, when the range is big it’s not memory efficient.
Can have something like
# These values can be changed
range = (2..6)
float_num = 5.3
# From http://stackoverflow.com/a/5083307/838346
float_is_int = float_num % 1 == 0
# Result
float_is_int && range.cover?(float_num)
1 Like