Module: Mint::RangeStepPatch Private

Defined in:
lib/minting/mint/dsl/range.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Ruby < 4.0’s Range#step calls rb_to_int on non-Numeric step arguments, which raises TypeError for Money objects. Ruby 4.0+ uses arithmetic iteration (+ / <=>) for non-numeric steps natively, so this patch is only needed on older Rubies.

Instance Method Summary collapse

Instance Method Details

#step(step_size = nil, &block) ⇒ self, Enumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterates over the range using a Money step value. Overrides Range#step to handle Mint::Money step sizes on Ruby < 4.0.

Parameters:

  • step_size (Mint::Money, nil) (defaults to: nil)

    step amount

Returns:

  • (self, Enumerator)

    self if block given, Enumerator otherwise

Raises:

  • (TypeError)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/minting/mint/dsl/range.rb', line 17

def step(step_size = nil, &block)
  return super unless step_size.is_a?(Mint::Money)

  raise TypeError, "can't iterate from NilClass" unless self.begin
  raise ArgumentError, "step can't be 0" if step_size.zero?

  if block
    each_money_step(step_size, &block)
    self
  else
    Enumerator.new { |yielder| each_money_step(step_size) { |value| yielder << value } }
  end
end