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) ⇒ Object

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.

Raises:

  • (TypeError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/minting/mint/dsl/range.rb', line 12

def step(step_size = nil, &)
  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_given?
    each_money_step(step_size, &)
    self
  else
    Enumerator.new do |yielder|
      each_money_step(step_size) { |v| yielder << v }
    end
  end
end