rogerdpack-sane 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/enumerable-extra.rb +149 -0
  2. data/lib/sane.rb +16 -2
  3. metadata +2 -1
@@ -0,0 +1,149 @@
1
+ module Enumerable
2
+ EXTRA_VERSION = '0.1.0'
3
+
4
+ alias old_map map
5
+ alias old_collect collect
6
+
7
+ # Returns the numeric total of the elements of +enum+, using +total+ as
8
+ # an accumulator (0 by default). Raises an error if any of the elements
9
+ # are non-numeric.
10
+ #
11
+ def sum(total = 0)
12
+ each{ |val| total += val }
13
+ total
14
+ end
15
+
16
+ # Returns a new array containing the results of running +method+ once for
17
+ # every element in the enumerable object. If both arguments and a block
18
+ # are provided the arguments are processed first, then passed to
19
+ # the block.
20
+ #
21
+ # If no method is provided, then it behaves as the standard MRI method.
22
+ #
23
+ # Examples:
24
+ #
25
+ # array = ['foo', 'bar']
26
+ #
27
+ # # No arguments
28
+ # array.map(:capitalize) => ['Foo', 'Bar']
29
+ #
30
+ # # With arguments
31
+ # array.map(:+, 'x') => ['foox', 'barx']
32
+ #
33
+ # # With arguments and a block
34
+ # array.map(:capitalize){ |e| e + 'x' } => ['Foox', 'Barx']
35
+ #
36
+ def map(method=nil, *args, &block)
37
+ if method
38
+ array = []
39
+ method = method.to_sym unless method.is_a?(Symbol)
40
+
41
+ each{ |obj|
42
+ temp = obj.send(method, *args)
43
+ if block
44
+ array << block.call(temp)
45
+ else
46
+ array << temp
47
+ end
48
+ }
49
+
50
+ array
51
+ else
52
+ old_map(&block)
53
+ end
54
+ end
55
+
56
+ # Reset the aliases
57
+ alias collect map
58
+ end
59
+
60
+ class Array
61
+ alias old_map map
62
+ alias old_map! map!
63
+ alias old_collect collect
64
+ alias old_collect! collect!
65
+ #alias old_select select
66
+
67
+ # Returns a new array containing the results of running +block+ once for
68
+ # every element in the +array+.
69
+ #
70
+ # Examples:
71
+ #
72
+ # array = ['foo', 'bar']
73
+ #
74
+ # # No arguments
75
+ # array.map(:capitalize) => ['Foo', 'Bar']
76
+ #
77
+ # # With arguments
78
+ # array.map(:+, 'x') => ['foox', 'barx']
79
+ #
80
+ # # With arguments and a block
81
+ # array.map(:capitalize){ |e| e + 'x' } => ['Foox', 'Barx']
82
+ #--
83
+ # The Array class actually has its own implementation of the +map+ method,
84
+ # hence the duplication.
85
+ #
86
+ def map(method=nil, *args, &block)
87
+ if method
88
+ array = []
89
+ method = method.to_sym unless method.is_a?(Symbol)
90
+
91
+ each{ |obj|
92
+ temp = obj.send(method, *args)
93
+ if block
94
+ array << block.call(temp)
95
+ else
96
+ array << temp
97
+ end
98
+ }
99
+ array
100
+ else
101
+ old_map(&block)
102
+ end
103
+ end
104
+
105
+ # Same as Array#map, but modifies the receiver in place. Also note that
106
+ # a block is _not_ required. If no block is given, an array of values
107
+ # is returned instead
108
+ #
109
+ def map!(method=nil, *args, &block)
110
+ self.replace(map(method, *args, &block))
111
+ end
112
+
113
+ =begin
114
+ def select(method=nil, condition = nil, *args, &block)
115
+ array = [] unless block
116
+ if method
117
+ if block
118
+ warn 'block ignored when arguments provided'
119
+ end
120
+
121
+ if condition.nil?
122
+ raise 'condition must be provided if method is provided'
123
+ end
124
+
125
+ method = method.to_sym unless method.is_a?(Symbol)
126
+
127
+ each{ |obj|
128
+ if args.length > 0
129
+ if obj.send(method, condition, *args)
130
+ array << obj
131
+ end
132
+ else
133
+ if obj.send(method, condition)
134
+ array << obj
135
+ end
136
+ end
137
+ }
138
+
139
+ return array
140
+ else
141
+ old_select(&block)
142
+ end
143
+ end
144
+ =end
145
+
146
+ # Reset the aliases
147
+ alias collect map
148
+ alias collect! map!
149
+ end
data/lib/sane.rb CHANGED
@@ -1,6 +1,8 @@
1
-
2
1
  # currently accepts either a glob [something with * in it]
3
2
  # or a filename
3
+ # TODO depend on require_all once they fix their issues
4
+ # and also don't load themselves apropo on 1.9, etc. etc.
5
+ # which I'm not too worried about anyway tho
4
6
  def require_rel glob # we don't allow for requiring directories currently :)
5
7
  dir = File.dirname(caller[0]) + '/'
6
8
  if glob.include? '*'
@@ -39,9 +41,21 @@ doctest: Also, like a normal require, you can leave off the .rb suffix
39
41
 
40
42
 
41
43
  class Object
44
+
45
+ # a helper for collection.include?
42
46
  def in? collection
43
47
  collection.include?(self)
44
48
  end
49
+
50
+ # assert(some statement)
51
+ # or
52
+ # assert(some statement, "some helper string")
53
+ def assert(should_be_true, string = nil)
54
+ if(!should_be_true)
55
+ raise "assertion failed #{string}"
56
+ end
57
+ end
58
+
45
59
  end
46
60
 
47
- require_rel 'enumerable-extra'
61
+ require_rel 'enumerable-extra' # for #map :symbol
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rogerdpack-sane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger PacK
@@ -25,6 +25,7 @@ extra_rdoc_files:
25
25
  files:
26
26
  - README
27
27
  - lib/sane.rb
28
+ - lib/enumerable-extra.rb
28
29
  has_rdoc: false
29
30
  homepage:
30
31
  post_install_message: