sixarm_ruby_ramp 4.1.0 → 4.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a60c19644a07626f66aae272432fb95dc384d5f3
4
- data.tar.gz: 289def436e9f4683c32d22baad4e42567aa69dbf
3
+ metadata.gz: a821169d3c371f60d7d48f2084ee1b6cceceecd9
4
+ data.tar.gz: 96510d04fe9d8bb4d695c3e101cab563653310c6
5
5
  SHA512:
6
- metadata.gz: 3ba91da4fcfc8e1bfba53b6375623f2e9849c0f97f0629da4cecc6d29e7b96d38594709d82b117593566d8125cf61ab725dbb22f2949cd7880f5de1c3713e3cb
7
- data.tar.gz: b8af5a9742251db4518c10fec767c61ee9e9ec1910e3e203aa9615b6a0c146efdfb18aa79d0793925a43ba4cc5b796b16cdbc87871465e2b7e5363fe1710e66d
6
+ metadata.gz: a5ece89814f03fd98c3c2f1d87d682828a90ba79e48d7a4122be16bc2af3ee208ab36f2de4c0ef3d247c8c65af482b46a3710a6a980cf80bf66eb037de170209
7
+ data.tar.gz: 601811bc53253b62be8c3c8ef88f653f78c72c9bc6101954c7109a3bc91369141fa543de13910fc3ead9981b8145c408442c7f83b430d4e59ef6ea28e0582352
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -28,7 +28,7 @@ Install:
28
28
 
29
29
  Bundler:
30
30
 
31
- gem "sixarm_ruby_ramp", ">=4.1.0", "<5"
31
+ gem "sixarm_ruby_ramp", ">=4.2.2", "<5"
32
32
 
33
33
  Require:
34
34
 
@@ -54,7 +54,6 @@ To install with high security:
54
54
  * choice, choices: one or more random elements from an array
55
55
  * cross: return the cross pairings of an array with another array
56
56
  * divvy: divides an array, like a pie, into a specified number of slices (deprecated - see method rdoc)
57
- * join, join_prefix_suffix, join_prefix_suffix_infix: like Array#join with improvements
58
57
  * onto: return a hash that maps an array's keys on to another array's values
59
58
  * rotate: moves the first element of an array to the end
60
59
  * rest: return the rest of the items of the array (aka cdr, aka shifted)
@@ -67,6 +66,13 @@ To install with high security:
67
66
  * union: builds an array containing each of the unique elements of sub-arrays ([[1,2,3,4],[2,3,4,5],[3,4,5,6]].union => [1,2,3,4,5,6])
68
67
 
69
68
 
69
+ ### Array join
70
+
71
+ * join: enhances #join with optional prefix, suffix, and infix.
72
+ * join_prefix_suffix: join with a prefix and suffix.
73
+ * join_prefix_suffix_infix: join with a prefix, suffix, and infix.
74
+
75
+
70
76
  ## Class
71
77
 
72
78
  * publicize_methods: make all methods public for a block, e.g. to unit test private methods
@@ -93,13 +99,28 @@ To install with high security:
93
99
  * join: forwards to self.to_a.join
94
100
  * to_h: convert the object to a hash by mapping each item to a key=>value pair.
95
101
  * to_h_merge: convert the object to a hash by mapping each item to a key=>value pair, merging any duplicate keys.
102
+ * power_set: return an array with all subsets of the enum's elements
103
+
104
+
105
+ ### Enumerable map
106
+
96
107
  * map_id: return the id of an Enumerable object; *requires* that the object respond to an 'id' message
97
108
  * map_to_a, map_to_f, map_to_i, map_to_s, map_to_sym: convert each object to a specific type by calling its respective method to_a, to_i, to_f, to_s, to_sym
98
109
  * map_with_index: for each item, yield to a block with the item and its incrementing index
99
- * nitems_until, select_until: return the number of, or an array containing, the leading elements for which block is false or nil.
100
- * nitems_while, select_while: return the number of items, or an array containing the leading elements, for which block is not false or nil.
101
- * nitems_with_index, select_with_index: calls block with two arguments, the item and its index, for each item in enum. Return the number of, or an array containing, the leading elements for which block is not false or nil.
102
- * power_set: return an array with all subsets of the enum's elements
110
+
111
+
112
+ ### Enumerable nitems
113
+
114
+ * nitems_while: the number of leading elements for which block is truthy.
115
+ * nitems_until: the number of leading elements for which block is falsey.
116
+ * nitems_with_index: the number of leading elements for which block with index is truthy.
117
+
118
+
119
+ ### Enumerable select
120
+
121
+ * select_while: return an array of the leading elements for which block is truthy.
122
+ * select_until: return an array of the leading elements for which block is falsey.
123
+ * select_with_index: return an array of the leading elements for which block with index is truthy.
103
124
 
104
125
 
105
126
  ## File
@@ -131,6 +152,7 @@ To install with high security:
131
152
 
132
153
  ## Integer
133
154
 
155
+ * rbit: reverse bit
134
156
  * maps: syntactic sugar to yield n times to a block, returning an array of any results
135
157
  * odd?: is the number odd?
136
158
 
@@ -229,6 +251,8 @@ Extensions that help debug Ruby programs.
229
251
 
230
252
  ## Changes
231
253
 
254
+ * 2015-03-14 4.2.1 Conditionally load `Array#shuffle*`
255
+ * 2015-03-14 4.2.0 Add `Integer#rbit`
232
256
  * 2015-01-13 4.1.0 Fix Enumerable `to_h` and add `to_h_merge`
233
257
  * 2015-01-13 4.0.1 Fix for Process.ps keys
234
258
  * 2014-12-17 4.0.0 Add `Pairable` methods; change semantics of `Hash#map_pair`; change `to_h` for Ruby 2.2.0
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.1.0
1
+ 4.2.2
@@ -0,0 +1,67 @@
1
+ class Array
2
+
3
+ # Alias join because we're going to override it
4
+ alias :ruby_join :join
5
+
6
+ # Concatenate the items into a string by join.
7
+ #
8
+ # @return [String] concatenated string
9
+ #
10
+ # @example Join with infix
11
+ # list=['a','b','c']
12
+ # list.join("*") => "a*b*c"
13
+ #
14
+ # @example Join with prefix and suffix
15
+ # list=['a','b','c']
16
+ # list.join("[","]") => "[a][b][c]"
17
+ #
18
+ # @example Join with prefix, suffix, and infix
19
+ # list=['a','b','c']
20
+ # list.join("*","[","]") => "[a]*[b]*[c]"
21
+ #
22
+ def join(*fixes)
23
+ if fixes.is_a?(String) then return self.ruby_join(fixes) end
24
+ return case fixes.size
25
+ when 0
26
+ ruby_join
27
+ when 1
28
+ ruby_join(fixes[0].to_s)
29
+ when 2
30
+ join_prefix_suffix(*fixes)
31
+ when 3
32
+ join_prefix_suffix_infix(*fixes)
33
+ else
34
+ raise ArgumentError, "join() takes 0-3 arguments; you gave #{fixes.size}]"
35
+ end
36
+ end
37
+
38
+ # Concatenate the items by joining using a prefix string and suffix string.
39
+ #
40
+ # @return [String] concatenated string
41
+ #
42
+ # @example
43
+ # list=['a','b','c']
44
+ # list.join("[","]") => "[a][b][c]"
45
+ #
46
+ def join_prefix_suffix(prefix, suffix)
47
+ prefix = prefix.to_s
48
+ suffix = suffix.to_s
49
+ return self.map{|item| prefix + item.to_s + suffix}.ruby_join()
50
+ end
51
+
52
+ # Concatenate the items by joining using a prefix string, suffix string, and infix string.
53
+ #
54
+ # @return [String] concatenated string
55
+ #
56
+ # @example
57
+ # list=['a','b','c']
58
+ # list.join("*","[","]") => "[a]*[b]*[c]"
59
+ #
60
+ def join_prefix_suffix_infix(prefix, suffix, infix)
61
+ prefix = prefix.to_s
62
+ suffix = suffix.to_s
63
+ infix = infix.to_s
64
+ return self.map{|item| prefix + item.to_s + suffix}.ruby_join(infix)
65
+ end
66
+
67
+ end
@@ -0,0 +1,49 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class Array
4
+
5
+ unless method_defined? :shuffle
6
+
7
+ # Randomly arrange the array items.
8
+ #
9
+ # @return [Array] a new array with the items shuffled.
10
+ #
11
+ # This implementation is optimized for speed, not for memory use.
12
+ # See http://codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/
13
+ #
14
+ # @example
15
+ # list=
16
+ # list=['a','b','c']
17
+ # list.shuffle!
18
+ # list => ['c','a','b']
19
+
20
+ def shuffle
21
+ dup.shuffle!
22
+ end
23
+
24
+ end
25
+
26
+ unless method_defined? :shuffle!
27
+
28
+ # Randomly arrange the array items.
29
+ #
30
+ # @return [Array] the array, with its items shuffled.
31
+ #
32
+ # This implementation is optimized for speed, not for memory use.
33
+ # See http://codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/
34
+ #
35
+ # @example
36
+ # list=
37
+ # list=['a','b','c']
38
+ # list.shuffle!
39
+ # list => ['c','a','b']
40
+
41
+ def shuffle!
42
+ each_index do |i|
43
+ j = rand(length-i) + i
44
+ self[j], self[i] = self[i], self[j]
45
+ end
46
+ end
47
+ end
48
+
49
+ end
@@ -1,78 +1,8 @@
1
1
  # -*- coding: utf-8 -*-
2
-
3
2
  require 'csv'
4
-
5
- # Array extensions
6
3
 
7
4
  class Array
8
5
 
9
-
10
- # Alias join because we're going to override it
11
-
12
- alias :ruby_join :join
13
-
14
-
15
- # Concatenate the items into a string by join.
16
- #
17
- # @return [String] concatenated string
18
- #
19
- # @example Join with infix
20
- # list=['a','b','c']
21
- # list.join("*") => "a*b*c"
22
- #
23
- # @example Join with prefix and suffix
24
- # list=['a','b','c']
25
- # list.join("[","]") => "[a][b][c]"
26
- #
27
- # @example Join with prefix, suffix, and infix
28
- # list=['a','b','c']
29
- # list.join("*","[","]") => "[a]*[b]*[c]"
30
- #
31
- def join(*fixes)
32
- if fixes.is_a?(String) then return self.ruby_join(fixes) end
33
- return case fixes.size
34
- when 0
35
- ruby_join
36
- when 1
37
- ruby_join(fixes[0].to_s)
38
- when 2
39
- join_prefix_suffix(*fixes)
40
- when 3
41
- join_prefix_suffix_infix(*fixes)
42
- else
43
- raise ArgumentError, "join() takes 0-3 arguments; you gave #{fixes.size}]"
44
- end
45
- end
46
-
47
- # Concatenate the items by joining using a prefix string and suffix string.
48
- #
49
- # @return [String] concatenated string
50
- #
51
- # @example
52
- # list=['a','b','c']
53
- # list.join("[","]") => "[a][b][c]"
54
- #
55
- def join_prefix_suffix(prefix, suffix)
56
- prefix = prefix.to_s
57
- suffix = suffix.to_s
58
- return self.map{|item| prefix + item.to_s + suffix}.ruby_join()
59
- end
60
-
61
- # Concatenate the items by joining using a prefix string, suffix string, and infix string.
62
- #
63
- # @return [String] concatenated string
64
- #
65
- # @example
66
- # list=['a','b','c']
67
- # list.join("*","[","]") => "[a]*[b]*[c]"
68
- #
69
- def join_prefix_suffix_infix(prefix, suffix, infix)
70
- prefix = prefix.to_s
71
- suffix = suffix.to_s
72
- infix = infix.to_s
73
- return self.map{|item| prefix + item.to_s + suffix}.ruby_join(infix)
74
- end
75
-
76
6
  # @return [Boolean] true if size > 0
77
7
  #
78
8
  # @example
@@ -92,7 +22,7 @@ class Array
92
22
  # [].rotate! => []
93
23
  #
94
24
  # @return [Array] self
95
-
25
+
96
26
  def rotate!
97
27
  if size>0
98
28
  push item=shift
@@ -100,7 +30,7 @@ class Array
100
30
  self
101
31
  end
102
32
 
103
-
33
+
104
34
  # @return [Object] a random item from the array
105
35
  #
106
36
  # @example
@@ -109,7 +39,7 @@ class Array
109
39
  # [1,2,3,4].choice => 3
110
40
  #
111
41
  # Implemented in Ruby 1.9
112
-
42
+
113
43
  def choice
114
44
  self[Kernel.rand(size)]
115
45
  end
@@ -120,7 +50,7 @@ class Array
120
50
  # @example
121
51
  # [1,2,3,4].choices(2) => [3,1]
122
52
  # [1,2,3,4].choices(3) => [4,2,3]
123
-
53
+
124
54
  def choices(count)
125
55
  arr = Array.new
126
56
  count.times { arr << self.choice }
@@ -139,13 +69,13 @@ class Array
139
69
  # This is identical to calling foo.zip(values).to_h
140
70
 
141
71
  def onto(values)
142
- size==values.size or raise ArgumentError, "Array size #{size} must match values size #{size}"
72
+ size==values.size or raise ArgumentError, "Array size #{size} must match values size #{size}"
143
73
  zip(values).to_h
144
74
  end
145
75
 
146
76
 
147
77
  ##############################################################
148
- #
78
+ #
149
79
  # GROUPINGS
150
80
  #
151
81
  ##############################################################
@@ -166,12 +96,12 @@ class Array
166
96
 
167
97
 
168
98
  ##############################################################
169
- #
99
+ #
170
100
  # COMBINATIONS
171
101
  #
172
102
  ##############################################################
173
103
 
174
-
104
+
175
105
  # @return [Array] the union of the array's items.
176
106
  #
177
107
  # In typical use, each item is an array.
@@ -212,10 +142,10 @@ class Array
212
142
  inject{|inj,item| inj & item.to_a } || []
213
143
  end
214
144
 
215
-
145
+
216
146
 
217
147
  ##############################################################
218
- #
148
+ #
219
149
  # LIST PROCESSING
220
150
  #
221
151
  ##############################################################
@@ -256,7 +186,7 @@ class Array
256
186
  # Delete the first _number_of_items_ items.
257
187
  #
258
188
  # @return [Array] the array, minus the deleted items.
259
- #
189
+ #
260
190
  # @example
261
191
  # list=['a','b','c']
262
192
  # list.shifted!
@@ -280,45 +210,8 @@ class Array
280
210
  alias :rest! :shifted!
281
211
 
282
212
 
283
- # Randomly arrange the array items.
284
- #
285
- # @return [Array] the array, with its items shuffled.
286
- #
287
- # This implementation is optimized for speed, not for memory use.
288
- # See http://codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/
289
- #
290
- # @example
291
- # list=
292
- # list=['a','b','c']
293
- # list.shuffle!
294
- # list => ['c','a','b']
295
-
296
- def shuffle!
297
- each_index do |i|
298
- j = rand(length-i) + i
299
- self[j], self[i] = self[i], self[j]
300
- end
301
- end
302
-
303
-
304
- # @return [Array] a new array with the items shuffled.
305
- #
306
- # This implementation is optimized for speed, not for memory use.
307
- # See http://codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/
308
- #
309
- # @example
310
- # list=
311
- # list=['a','b','c']
312
- # list.shuffle!
313
- # list => ['c','a','b']
314
-
315
- def shuffle
316
- dup.shuffle!
317
- end
318
-
319
-
320
213
  ##############################################################
321
- #
214
+ #
322
215
  # CASTS
323
216
  #
324
217
  ##############################################################
@@ -334,7 +227,7 @@ class Array
334
227
  # [[1,2,3],[4,5,6]] => "1,2,3\n4,5,6\n"
335
228
  #
336
229
  # @example of a blank array
337
- #
230
+ #
338
231
  # [].to_csv => ""
339
232
  #
340
233
  # N.b. this method uses the multi-dimensional if the
@@ -368,7 +261,7 @@ class Array
368
261
  # Each subarray becomes one 'line' in the output.
369
262
  #
370
263
  # @example of a blank array
371
- #
264
+ #
372
265
  # [].to_csv => ""
373
266
 
374
267
  def to_tsv(ops={})
@@ -0,0 +1,114 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Enumerable
3
+
4
+ # Map each item => item.id
5
+ #
6
+ # @return [Enumerable<Object>] an list of each item.id
7
+ #
8
+ # @example
9
+ # users = User.find(:all)
10
+ # users.map_id => [1,2,3,4,...]
11
+ #
12
+ # A typical use is to convert a list of ActiveRecord items to a list of id items.
13
+ #
14
+ # This method is a faster way to get the same results as items.map(&:id)
15
+ #
16
+ def map_id
17
+ map{|item| item.id}
18
+ end
19
+
20
+ # Map each item => item.to_a
21
+ #
22
+ # @return [Enumberable<Array<Object>>] a list of each item.to_a
23
+ #
24
+ # @example
25
+ # hashes = [{1=>2, 3=>4},{5=>6, 7=>8}]
26
+ # hashes.map_to_a => [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
27
+ #
28
+ # This method is a faster way to get the same results as items.map(&:to_a)
29
+ #
30
+ def map_to_a
31
+ map{|item| item.to_a}
32
+ end
33
+
34
+ # Map each item => item.to_f
35
+ #
36
+ # @return [Enumerable<Float>] a list of each item.to_f
37
+ #
38
+ # @example
39
+ # strings = ["1","2","3"]
40
+ # strings.map_to_f => [1.0, 2.0, 3.0]
41
+ #
42
+ # A typical use is to convert a list of String items to a list of float items.
43
+ #
44
+ # This method is a fast way to get the same results as items.map(&:to_f)
45
+ #
46
+ def map_to_f
47
+ map{|item| item.to_f}
48
+ end
49
+
50
+ # Map each item => item.to_i
51
+ #
52
+ # @return [Enumerable<Integer>] a list of each item.to_i
53
+ #
54
+ # @example
55
+ # strings = ["1","2","3"]
56
+ # strings.map_to_i => [1, 2, 3]
57
+ #
58
+ # A typical use is to convert a list of String items to a list of integer items.
59
+ #
60
+ # This method is a fast way to get the same results as items.map(&:to_i)
61
+ #
62
+ def map_to_i
63
+ map{|item| item.to_i}
64
+ end
65
+
66
+ # Map each item => item.to_s
67
+ #
68
+ # @return [Enumerable<String>] a list of each item.to_s
69
+ #
70
+ # @example
71
+ # numbers = [1, 2, 3]
72
+ # numbers.map_to_s => ["1", "2", "3"]
73
+ #
74
+ # A typical use is to convert a list of Numeric items to a list of String items.
75
+ #
76
+ # This method is a fast way to get the same results as items.map(&:to_s)
77
+ #
78
+ def map_to_s
79
+ map{|item| item.to_s}
80
+ end
81
+
82
+ # Map each item => item.to_sym
83
+ #
84
+ # @return [Enumerable<Symbol>] a list of each item.to_sym
85
+ #
86
+ # @example
87
+ # strings = ["foo", "goo", "hoo"]
88
+ # strings.map_to_sym => [:foo, :goo, :hoo]
89
+ #
90
+ # A typical use is to convert a list of Object items to a list of Symbol items.
91
+ #
92
+ # This method is a fast way to get the same results as items.map(&:to_sym)
93
+ #
94
+ def map_to_sym
95
+ map{|item| item.to_sym}
96
+ end
97
+
98
+ # Map each item and its index => a new output
99
+ #
100
+ # @return [Enumerable<Object>] an list of each item transformed by the block
101
+ # @see Enumerable#map
102
+ # @see Enumerable#each_with_index
103
+ #
104
+ # @example
105
+ # strings = ["a", "b", "c"]
106
+ # strings.map_with_index{|string,index| "#{string}#{index}"}
107
+ # => ["a0, "b1", "c3"]
108
+ #
109
+ def map_with_index
110
+ index=-1
111
+ map{|item| index+=1; yield(item,index)}
112
+ end
113
+
114
+ end
@@ -0,0 +1,66 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Enumerable
3
+
4
+ # @example
5
+ # enum.nitems?(n) {| obj | block }
6
+ # => true iff the block is not false or nil num times
7
+ #
8
+ # @return [Boolean] true iff the block is not false or nil num times
9
+ #
10
+ def nitems?(n)
11
+ num = 0
12
+ each{|item|
13
+ if yield(item)
14
+ num+=1
15
+ if num > n then return false end
16
+ end
17
+ }
18
+ return num==n
19
+ end
20
+
21
+ # @example
22
+ # enum.nitems_while {| obj | block }
23
+ # => number of items
24
+ #
25
+ # @return [Integer] the number of leading elements for which block is not false or nil.
26
+ #
27
+ def nitems_while
28
+ num = 0
29
+ each{|item| yield(item) ? (num+=1) : break}
30
+ return num
31
+ end
32
+
33
+ # @example
34
+ # enum.nitems_until {| obj | block }
35
+ # => number of items
36
+ #
37
+ # @return [Integer] the number of leading elements for which block is false.
38
+ #
39
+ def nitems_until
40
+ num = 0
41
+ each{|item|
42
+ if yield(item)
43
+ break
44
+ else
45
+ num+=1
46
+ end
47
+ }
48
+ return num
49
+ end
50
+
51
+
52
+ # Calls block with two arguments, the item and its index, for each item in enum.
53
+ #
54
+ # @example
55
+ # enum.nitems_with_index {|obj,i| block }
56
+ # => number of items
57
+ #
58
+ # @return [Integer] the number of leading elements for which block is true.
59
+ #
60
+ def nitems_with_index
61
+ index = 0
62
+ each{|item| yield(item,index) ? (index+=1) : break}
63
+ return index
64
+ end
65
+
66
+ end
@@ -0,0 +1,50 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Enumerable
3
+
4
+ # @example
5
+ # enum.select_while {|obj| block }
6
+ # => array
7
+ #
8
+ # @return [Array<Object>] the leading elements for which block is not false or nil.
9
+ #
10
+ def select_while
11
+ arr = []
12
+ each{|item| yield(item) ? (arr << item) : break}
13
+ return arr
14
+ end
15
+
16
+ # @example
17
+ # enum.select_until {|obj| block }
18
+ # => array
19
+ #
20
+ # @return [Array<Object>] the leading elements for which block is false or nil.
21
+ #
22
+ def select_until
23
+ arr = []
24
+ each{|item| yield(item) ? break : (arr << item)}
25
+ return arr
26
+ end
27
+
28
+ # Calls block with two arguments, the item and its index, for each item in enum.
29
+ #
30
+ # @example
31
+ # enum.select_with_index {|obj,i| block }
32
+ # => array
33
+ #
34
+ # @return [Array<Object> the leading elements for which block is not false or nil.
35
+ #
36
+ def select_with_index
37
+ index = 0
38
+ arr = []
39
+ each{|item|
40
+ if yield(item,index)
41
+ arr << item
42
+ index+=1
43
+ else
44
+ break
45
+ end
46
+ }
47
+ return arr
48
+ end
49
+
50
+ end