aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/range/blockless_step.rb
blob: 4d5e27457b6536fc08d9b4815c0eaf0090567a97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Range #:nodoc:
      # Return and array when step is called without a block
      module BlocklessStep

        def self.included(klass) #:nodoc:
          klass.send(:alias_method, :step_with_block, :step)
          klass.send(:alias_method, :step, :step_without_block)
        end        
        
        def step_without_block(value, &block)
          if block_given?
            step_with_block(value, &block)
          else
            returning [] do |array|
              step_with_block(value) {|step| array << step }
            end
          end
        end

      end
    end
  end
end