enumerable-extra 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES CHANGED
@@ -1,2 +1,10 @@
1
- = 0.1.0 - 14-May-2009
2
- * Initial release
1
+ = 0.1.1 - 18-Jul-2009
2
+ * Added 1.9.x compatibility. Note that Enumerator objects are explicitly
3
+ converted back to arrays to match the 1.8.x behavior.
4
+ * Changed license to Artistic 2.0, and added it explictly to the gemspec.
5
+ * Removed the required_ruby_version from the gemspec since it's now
6
+ compatible.
7
+ * Updated the README, including a missing warranty disclaimer (whoops).
8
+
9
+ = 0.1.0 - 14-May-2009
10
+ * Initial release
data/MANIFEST CHANGED
@@ -1,7 +1,7 @@
1
- * CHANGES
2
- * README
3
- * MANIFEST
4
- * Rakefile
5
- * enumerable-extra.gemspec
6
- * lib/enumerable/extra
7
- * test/tc_enumerable_extra.rb
1
+ * CHANGES
2
+ * README
3
+ * MANIFEST
4
+ * Rakefile
5
+ * enumerable-extra.gemspec
6
+ * lib/enumerable/extra
7
+ * test/tc_enumerable_extra.rb
data/README CHANGED
@@ -1,54 +1,61 @@
1
- = Description
2
- This library includes modified versions of the Enumerable methods, designed
3
- to make list comprehensions a little bit easier and prettier in Ruby.
4
-
5
- = Installation
6
- rake test (optional)
7
- rake install (non-gem) OR rake install_gem (gem)
8
-
9
- = Synopsis
10
- require 'enumerable/extra'
11
-
12
- array = %w/foo bar baz/
13
-
14
- array.map(:upcase) => ['FOO', 'BAR', 'BAZ']
15
- array.map(:+, 'A') => ['fooA', 'barA', 'bazA']
16
-
17
- numbers = [1,2,3]
18
- numbers.sum => 6
19
-
20
- = Motivation
21
- This library was created in reaction to the ugly "&" (or worse, "&its")
22
- notation started by Ruby on Rails and perpetuated by the Symbol#to_proc
23
- adherents.
24
-
25
- The theory behind Symbol#to_proc is that it's a generic metaprogramming
26
- solution that will solve a certain range of programming problems. The
27
- reality is that 99% of people use it for list comprehensions*. So, instead
28
- of introducing crappy notation, I decided that it made better sense to
29
- modify Enumerable methods to accept arguments.
30
-
31
- There are two advantages to this. First, superior notation, i.e. no need
32
- for the ampersand. One of the reasons I chose Ruby as my primary programming
33
- language in the first place was the beauty of its notation. I don't want
34
- to see that ruined by Symbol#to_proc. Also, coming from a C background, I
35
- find the ampersand too reminiscent of C address notation.
36
-
37
- Second, Symbol#to_proc is very slow.
38
-
39
- * Based on the questions and solutions that I see on the ruby-talk and rails
40
- mailing lists. I've monitored the former for almost seven years and the
41
- latter for close to two now. This is in addition to many blogs I read that
42
- occasionally touch on the subject.
43
-
44
- = Future Plans
45
- Modify several more Enumerable methods.
46
-
47
- = License
48
- Ruby's
49
-
50
- = Warranty
51
-
52
- = Author
53
- Daniel J. Berger
54
- djberg96 at nospam at gmail dot com
1
+ = Description
2
+ This library includes modified versions of the Enumerable methods, designed
3
+ to make list comprehensions a little bit easier and prettier in Ruby.
4
+
5
+ = Installation
6
+ rake test (optional)
7
+ rake install (non-gem) OR rake install_gem (gem)
8
+
9
+ = Synopsis
10
+ require 'enumerable/extra'
11
+
12
+ array = %w/foo bar baz/
13
+
14
+ array.map(:upcase) => ['FOO', 'BAR', 'BAZ']
15
+ array.map(:+, 'A') => ['fooA', 'barA', 'bazA']
16
+
17
+ numbers = [1,2,3]
18
+ numbers.sum => 6
19
+
20
+ = Motivation
21
+ This library was created in reaction to the ugly "&" (or worse, "&its")
22
+ notation started by Ruby on Rails and perpetuated by the Symbol#to_proc
23
+ adherents.
24
+
25
+ The theory behind Symbol#to_proc is that it's a generic metaprogramming
26
+ solution that will solve a certain range of programming problems. The
27
+ reality is that 99% of people use it for list comprehensions*. So, instead
28
+ of introducing crappy notation, I decided that it made better sense to
29
+ modify Enumerable methods to accept arguments.
30
+
31
+ There are two advantages to this. First, superior notation, i.e. no need
32
+ for the ampersand. One of the reasons I chose Ruby as my primary programming
33
+ language in the first place was the beauty of its notation. I don't want
34
+ to see that ruined by Symbol#to_proc. Also, coming from a C background, I
35
+ find the ampersand too reminiscent of C address notation.
36
+
37
+ Second, Symbol#to_proc is very slow.
38
+
39
+ Update: It seems Symbol#to_proc is reasonably fast now in the 1.9.x branch.
40
+ However, it does not allow you to pass arguments to a method. That means
41
+ you still can't do the equivalent of [1,2,3].map(:+, 1), for example.
42
+
43
+ * Based on the questions and solutions that I see on the ruby-talk and rails
44
+ mailing lists. I've monitored the former for almost seven years and the
45
+ latter for close to two now. This is in addition to many blogs I read that
46
+ occasionally touch on the subject.
47
+
48
+ = Future Plans
49
+ Modify several more Enumerable methods.
50
+
51
+ = License
52
+ Artistic 2.0
53
+
54
+ = Warranty
55
+ This package is provided "as is" and without any express or
56
+ implied warranties, including, without limitation, the implied
57
+ warranties of merchantability and fitness for a particular purpose
58
+
59
+ = Author
60
+ Daniel J. Berger
61
+ djberg96 at nospam at gmail dot com
@@ -1,149 +1,127 @@
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
1
+ module Enumerable
2
+ EXTRA_VERSION = '0.1.1'
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 argument is provided, then it behaves as the standard MRI
22
+ # method.
23
+ #
24
+ # Examples:
25
+ #
26
+ # array = ['foo', 'bar']
27
+ #
28
+ # # No arguments
29
+ # array.map(:capitalize) => ['Foo', 'Bar']
30
+ #
31
+ # # With arguments
32
+ # array.map(:+, 'x') => ['foox', 'barx']
33
+ #
34
+ # # With arguments and a block
35
+ # array.map(:capitalize){ |e| e + 'x' } => ['Foox', 'Barx']
36
+ #
37
+ # Note that for 1.9.x users, Enumerator objects are converted explicitly
38
+ # back into arrays.
39
+ #
40
+ def map(method=nil, *args, &block)
41
+ if method
42
+ array = []
43
+ method = method.to_sym unless method.is_a?(Symbol)
44
+
45
+ each{ |obj|
46
+ temp = obj.send(method, *args)
47
+ if block
48
+ array << block.call(temp)
49
+ else
50
+ array << temp
51
+ end
52
+ }
53
+
54
+ # Convert enumerators back to arrays for 1.9.x
55
+ RUBY_VERSION.to_f >= 1.9 ? array.to_a : array
56
+ else
57
+ RUBY_VERSION.to_f ? old_map(&block).to_a : old_map(&block)
58
+ end
59
+ end
60
+
61
+ # Reset the aliases
62
+ alias collect map
63
+ end
64
+
65
+ class Array
66
+ # These methods are defined separately in array.c, and they are not actual
67
+ # aliases, so we must alias them each separately from Enumerable.
68
+
69
+ alias old_map map
70
+ alias old_map! map!
71
+ alias old_collect collect
72
+ alias old_collect! collect!
73
+
74
+ # Returns a new array containing the results of running +block+ once for
75
+ # every element in the +array+.
76
+ #
77
+ # Examples:
78
+ #
79
+ # array = ['foo', 'bar']
80
+ #
81
+ # # No arguments
82
+ # array.map(:capitalize) => ['Foo', 'Bar']
83
+ #
84
+ # # With arguments
85
+ # array.map(:+, 'x') => ['foox', 'barx']
86
+ #
87
+ # # With arguments and a block
88
+ # array.map(:capitalize){ |e| e + 'x' } => ['Foox', 'Barx']
89
+ #
90
+ # Note that for 1.9.x users, Enumerator objects are converted explicitly
91
+ # back into arrays.
92
+ #--
93
+ # The Array class actually has its own implementation of the +map+ method,
94
+ # hence the duplication.
95
+ #
96
+ def map(method=nil, *args, &block)
97
+ if method
98
+ array = []
99
+ method = method.to_sym unless method.is_a?(Symbol)
100
+
101
+ each{ |obj|
102
+ temp = obj.send(method, *args)
103
+ if block
104
+ array << block.call(temp)
105
+ else
106
+ array << temp
107
+ end
108
+ }
109
+
110
+ RUBY_VERSION.to_f >= 1.9 ? array.to_a : array
111
+ else
112
+ RUBY_VERSION.to_f >= 1.9 ? old_map(&block).to_a : old_map(&block)
113
+ end
114
+ end
115
+
116
+ # Same as Array#map, but modifies the receiver in place. Also note that
117
+ # a block is _not_ required. If no block is given, an array of values
118
+ # is returned instead
119
+ #
120
+ def map!(method=nil, *args, &block)
121
+ self.replace(map(method, *args, &block))
122
+ end
123
+
124
+ # Reset the aliases
125
+ alias collect map
126
+ alias collect! map!
127
+ end
@@ -1,77 +1,77 @@
1
- ########################################################################
2
- # tc_enumerable_extra.rb
3
- #
4
- # Test case for the enumerable-extra library. You should run this
5
- # test via the 'rake test' task.
6
- ########################################################################
7
- require 'test/unit'
8
- require 'enumerable/extra'
9
-
10
- class TC_Enumerable_Extra < Test::Unit::TestCase
11
- def setup
12
- @words = %w/foo bar baz/
13
- @numbers = [1,2,3]
14
- @hash = {'foo', 1, 'bar', 2}
15
- @array = []
16
- end
17
-
18
- def test_version
19
- assert_equal('0.1.0', Enumerable::EXTRA_VERSION)
20
- end
21
-
22
- def test_sum
23
- assert_respond_to(@numbers, :sum)
24
- assert_equal(6, @numbers.sum)
25
- assert_equal(20, @numbers.sum(14))
26
- end
27
-
28
- def test_sum_expected_errors
29
- assert_raises(TypeError){ @words.sum }
30
- end
31
-
32
- def test_map_array_no_block
33
- assert_nothing_raised{ @words.map }
34
- assert_equal(%w/foo bar baz/, @words.map)
35
- assert_equal(%w/FOO BAR BAZ/, @words.map(:upcase))
36
- assert_equal(%w/fooA barA bazA/, @words.map(:+, 'A'))
37
- assert_equal(%w/foo bar baz/, @words) # Verify receiver unmodified
38
- end
39
-
40
- # Test the alias explicitly
41
- def test_collect_array_no_block
42
- assert_nothing_raised{ @words.collect }
43
- assert_equal(%w/foo bar baz/, @words.collect)
44
- assert_equal(%w/FOO BAR BAZ/, @words.collect(:upcase))
45
- assert_equal(%w/fooA barA bazA/, @words.collect(:+, 'A'))
46
- assert_equal(%w/foo bar baz/, @words) # Verify receiver unmodified
47
- end
48
-
49
- def test_map_bang_array_no_block
50
- assert_nothing_raised{ @words.map! }
51
- assert_equal(%w/foo bar baz/, @words.map!)
52
- assert_equal(%w/FOO BAR BAZ/, @words.map!(:upcase))
53
- assert_equal(%w/FOO BAR BAZ/, @words) # Verify receiver modified
54
- end
55
-
56
- def test_map_with_block
57
- assert_nothing_raised{ @words.map{} }
58
- assert_nothing_raised{ @words.map{ |e| @array << e } }
59
- assert_equal(%w/foo bar baz/, @array)
60
-
61
- @array = []
62
- assert_nothing_raised{ @words.map(:upcase){ |e| @array << e } }
63
- assert_equal(%w/FOO BAR BAZ/, @array)
64
-
65
- @array = []
66
- assert_nothing_raised{ @words.map(:+, 'A'){ |e| @array << e } }
67
- assert_equal(%w/fooA barA bazA/, @array)
68
- assert_equal(%w/foo bar baz/, @words) # Verify receiver unmodified
69
- end
70
-
71
- def teardown
72
- @words = nil
73
- @numbers = nil
74
- @hash = nil
75
- @array = nil
76
- end
77
- end
1
+ ########################################################################
2
+ # tc_enumerable_extra.rb
3
+ #
4
+ # Test case for the enumerable-extra library. You should run this
5
+ # test via the 'rake test' task.
6
+ ########################################################################
7
+ require 'test/unit'
8
+ require 'enumerable/extra'
9
+
10
+ class TC_Enumerable_Extra < Test::Unit::TestCase
11
+ def setup
12
+ @words = %w/foo bar baz/
13
+ @numbers = [1,2,3]
14
+ @hash = {'foo' => 1, 'bar' => 2}
15
+ @array = []
16
+ end
17
+
18
+ def test_version
19
+ assert_equal('0.1.1', Enumerable::EXTRA_VERSION)
20
+ end
21
+
22
+ def test_sum
23
+ assert_respond_to(@numbers, :sum)
24
+ assert_equal(6, @numbers.sum)
25
+ assert_equal(20, @numbers.sum(14))
26
+ end
27
+
28
+ def test_sum_expected_errors
29
+ assert_raises(TypeError){ @words.sum }
30
+ end
31
+
32
+ def test_map_array_no_block
33
+ assert_nothing_raised{ @words.map }
34
+ assert_equal(%w/foo bar baz/, @words.map)
35
+ assert_equal(%w/FOO BAR BAZ/, @words.map(:upcase))
36
+ assert_equal(%w/fooA barA bazA/, @words.map(:+, 'A'))
37
+ assert_equal(%w/foo bar baz/, @words) # Verify receiver unmodified
38
+ end
39
+
40
+ # Test the alias explicitly
41
+ def test_collect_array_no_block
42
+ assert_nothing_raised{ @words.collect }
43
+ assert_equal(%w/foo bar baz/, @words.collect)
44
+ assert_equal(%w/FOO BAR BAZ/, @words.collect(:upcase))
45
+ assert_equal(%w/fooA barA bazA/, @words.collect(:+, 'A'))
46
+ assert_equal(%w/foo bar baz/, @words) # Verify receiver unmodified
47
+ end
48
+
49
+ def test_map_bang_array_no_block
50
+ assert_nothing_raised{ @words.map! }
51
+ assert_equal(%w/foo bar baz/, @words.map!)
52
+ assert_equal(%w/FOO BAR BAZ/, @words.map!(:upcase))
53
+ assert_equal(%w/FOO BAR BAZ/, @words) # Verify receiver modified
54
+ end
55
+
56
+ def test_map_with_block
57
+ assert_nothing_raised{ @words.map{} }
58
+ assert_nothing_raised{ @words.map{ |e| @array << e } }
59
+ assert_equal(%w/foo bar baz/, @array)
60
+
61
+ @array = []
62
+ assert_nothing_raised{ @words.map(:upcase){ |e| @array << e } }
63
+ assert_equal(%w/FOO BAR BAZ/, @array)
64
+
65
+ @array = []
66
+ assert_nothing_raised{ @words.map(:+, 'A'){ |e| @array << e } }
67
+ assert_equal(%w/fooA barA bazA/, @array)
68
+ assert_equal(%w/foo bar baz/, @words) # Verify receiver unmodified
69
+ end
70
+
71
+ def teardown
72
+ @words = nil
73
+ @numbers = nil
74
+ @hash = nil
75
+ @array = nil
76
+ end
77
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerable-extra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Berger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-14 00:00:00 -06:00
12
+ date: 2009-07-18 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,8 +31,8 @@ files:
31
31
  - MANIFEST
32
32
  has_rdoc: true
33
33
  homepage: http://www.rubyforge.org/projects/shards
34
- licenses: []
35
-
34
+ licenses:
35
+ - Artistic 2.0
36
36
  post_install_message:
37
37
  rdoc_options: []
38
38
 
@@ -40,9 +40,9 @@ require_paths:
40
40
  - lib
41
41
  required_ruby_version: !ruby/object:Gem::Requirement
42
42
  requirements:
43
- - - <
43
+ - - ">="
44
44
  - !ruby/object:Gem::Version
45
- version: 1.9.0
45
+ version: "0"
46
46
  version:
47
47
  required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  requirements:
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  requirements: []
54
54
 
55
55
  rubyforge_project: shards
56
- rubygems_version: 1.3.3
56
+ rubygems_version: 1.3.4
57
57
  signing_key:
58
58
  specification_version: 3
59
59
  summary: Enhanced methods for Enumerable objects