public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
Reimplement collect_every, using _slice_ to process by subarray

The implementation is 1/3 of original, and the 2 cases (with/without 
block)
share the same logic.
Raul Parolari (author)
Mon Mar 03 17:44:19 -0800 2008
commit  a98dbe56b2e205adf948dd08c9119c8379463447
tree    8076e12bcb9783a33d2aab4be36f11d5e3cc1e21
parent  fa249934b244cf18d5a9b6647a5cc706afa31599
...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
 
 
 
 
 
 
 
 
 
 
60
61
62
63
64
 
 
 
 
 
 
 
 
 
65
...
20
21
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
24
25
26
27
28
29
30
31
32
33
34
 
 
 
35
36
37
38
39
40
41
42
43
44
45
0
@@ -20,44 +20,24 @@
0
 # b.collect_every(3,true,1) #=> [[2, 3, 4], [5, 6, 7]]
0
 
0
 class Array
0
- def collect_every(n,fill=false,offset=0)
0
-
0
- if block_given?
0
- while offset < size
0
- ret=[]
0
-
0
- if fill
0
- n.times do |x|
0
- if offset+x > size - 1: ret << nil
0
- else ret << self[offset+x] end
0
- end
0
- else
0
- n.times { |x| ret << self[offset+x] unless offset+x > size-1 }
0
- end
0
-
0
- offset += n
0
- yield ret
0
- ret = nil
0
- end
0
-
0
- else
0
- ret = []
0
- while offset < size
0
- ret << []
0
-
0
- if fill
0
- n.times do |x|
0
- if offset+x > size - 1: ret.last << nil
0
- else ret.last << self[offset+x] end
0
- end
0
- else
0
- n.times { |x| ret.last << self[offset+x] unless offset+x > size-1 }
0
- end
0
-
0
- offset += n
0
- end
0
- return ret
0
+
0
+ def collect_every(n, fill=false, offset=0)
0
+ result = [ ]
0
+
0
+ self.slice!(0, offset)
0
+
0
+ result << self.slice!(0, n) until self.empty?
0
+
0
+ if fill && !result.empty?
0
+ # ('result.last' cannot be assigned to; use array[i] access)
0
+ result[-1] += [nil] * (n - result[-1].size)
0
     end
0
-
0
- end
0
-end
0
\ No newline at end of file
0
+
0
+ if block_given? && !result.empty?
0
+ yield result.shift until result.empty?
0
+ end
0
+
0
+ result
0
+ end # collect_every
0
+
0
+end # class
0
\ No newline at end of file

Comments

    No one has commented yet.