This isn’t important, because I hope no-one is using an infinite step
value.
That said, in MRI Ruby versions >= 1.9.3 (and > 1.8.7?):
(5.0…5.0).step(8.0) { |v| puts v.inspect } #=> doesn’t yield anything
but
(5.0…5.0).step(1.0 / 0.0) { |v| puts v.inspect } #=> yields 5.0
A minor discrepancy, but still the infinite step should also not
yield anything?
Below is code which shows fuller examples.
puts “Ruby #step”
(puts RUBY_DESCRIPTION) if defined?(RUBY_DESCRIPTION)
p RUBY_VERSION
step_finite = 8.0
step_infinite = 1.0 / 0.0
def look_at_step(beginv, endv, stepv, stringv = nil)
print “# (#{beginv}).step(#{endv}, #{stepv}):”
beginv.step(endv, stepv) { |v| print " #{v.inspect}" }
puts
rangev = beginv…endv
print “# (#{rangev}).step(#{stepv}):”
rangev.step(stepv) { |v| print " #{v.inspect}" }
puts
rangev = beginv…endv
print “# (#{rangev}).step(#{stepv}):”
rangev.step(stepv) { |v| print " #{v.inspect}" }
puts
puts stringv if stringv
puts “# ended”
end
puts “# As expected, for step = 8.0 all these yield exactly one value:
5.0”
stepv = 8.0
look_at_step(5.0, 6.0, step_finite)
look_at_step(5.0, 6.0, step_infinite)
puts “# As do most of the following.”
textv = “As expected, (5.0…5.0).step(8.0) did not yield anything.”
look_at_step(5.0, 5.0, step_finite, textv)
textv = “#But - not as expected? - (5.0…5.0).step(Infinity) yielded
5.0.”
look_at_step(5.0, 5.0, step_infinite, textv)
puts “#”
puts “# Unless this is Ruby.1.8.7 (and below?): then infinity steps
yield NaN,”
puts “# except for the exclusive ranges when no values are yielded.”
puts “# Arguably for the exclusive range with the same begin and end
values,”
puts “# this is the ‘correct’ behaviour to be like
(5.0…5.0)step(8.0).”
exit