5b6484445948ebe063c264a1015dfcc4

Please see this ruby-talk thread for clarification: http://www.ruby-forum.com/topic/136507

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Enumerable
  def to_a(n = nil)
    result = []
    inclusion_condition = true
    return result if n.to_i <= 0 and !block_given?
    each do |*elements|
      elements = elements.first if elements.size == 1
      if block_given?
        values = result.last
        inclusion_condition = yield(*values) unless values.nil?
      else
        inclusion_condition = n != result.size
      end

      if inclusion_condition
        result << elements
      else
        result.pop if block_given?
        break result
      end
    end
    result
  end
end

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel