webget-enumerable_extend 1.1.2
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/lib/enumerable_extensions.rb +154 -0
- data/test/unit/enumerable_extensions_test.rb +117 -0
- metadata +53 -0
@@ -0,0 +1,154 @@
|
|
1
|
+
# = Enumerable Extensions
|
2
|
+
#
|
3
|
+
# Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
|
4
|
+
# Copyright:: Copyright (c) 2006-2008 Joel Parker Henderson
|
5
|
+
# License:: CreativeCommons License, Non-commercial Share Alike
|
6
|
+
# License:: LGPL, GNU Lesser General Public License
|
7
|
+
#
|
8
|
+
# Ruby Enumerable base class extensions.
|
9
|
+
#
|
10
|
+
##
|
11
|
+
|
12
|
+
module Enumerable
|
13
|
+
|
14
|
+
|
15
|
+
########################################################################
|
16
|
+
#
|
17
|
+
# map
|
18
|
+
#
|
19
|
+
########################################################################
|
20
|
+
|
21
|
+
# map item => item.id
|
22
|
+
def map_id
|
23
|
+
map{|x| x.id}
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# map item => item.to_sym
|
28
|
+
def map_to_sym
|
29
|
+
map{|x| x.to_sym}
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
########################################################################
|
34
|
+
#
|
35
|
+
# select
|
36
|
+
#
|
37
|
+
########################################################################
|
38
|
+
|
39
|
+
|
40
|
+
# enum.select_while {|obj| block } => array
|
41
|
+
# Returns an array containing the leading elements for which block is not false or nil.
|
42
|
+
def select_while
|
43
|
+
a = []
|
44
|
+
each{|x| yield(x) ? (a << x) : break}
|
45
|
+
return a
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
# enum.select_until {|obj| block } => array
|
50
|
+
# Returns an array containing the leading elements for which block is false or nil.
|
51
|
+
def select_until
|
52
|
+
a = []
|
53
|
+
each{|x| yield(x) ? break : (a << x)}
|
54
|
+
return a
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
# enum.select_with_index {|obj,i| block } => array
|
59
|
+
# Calls block with two arguments, the item and its index, for each item in enum.
|
60
|
+
# Returns an array containing the leading elements for which block is not false or nil.
|
61
|
+
def select_with_index
|
62
|
+
i = 0
|
63
|
+
a = []
|
64
|
+
each{|x|
|
65
|
+
if yield(x,i)
|
66
|
+
a << x
|
67
|
+
i+=1
|
68
|
+
else
|
69
|
+
break
|
70
|
+
end
|
71
|
+
}
|
72
|
+
return a
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
########################################################################
|
77
|
+
#
|
78
|
+
# nitems
|
79
|
+
#
|
80
|
+
########################################################################
|
81
|
+
|
82
|
+
|
83
|
+
# enum.nitems_while {| obj | block } => number of items
|
84
|
+
# Returns the number of leading elements for which block is not false or nil.
|
85
|
+
def nitems_while
|
86
|
+
n = 0
|
87
|
+
each{|x| yield(x) ? (n+=1) : break}
|
88
|
+
return n
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
# enum.nitems_until {| obj | block } => number of items
|
93
|
+
# Returns the number of leading elements for which block is false.
|
94
|
+
def nitems_until
|
95
|
+
n = 0
|
96
|
+
each{|x|
|
97
|
+
if yield(x)
|
98
|
+
break
|
99
|
+
else
|
100
|
+
n+=1
|
101
|
+
end
|
102
|
+
}
|
103
|
+
return n
|
104
|
+
irb
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
# enum.nitems_with_index {|obj,i| block } => number of items
|
109
|
+
# Calls block with two arguments, the item and its index, for each item in enum.
|
110
|
+
# Returns the number of leading elements for which block is true.
|
111
|
+
def nitems_with_index
|
112
|
+
i = 0
|
113
|
+
each{|x| yield(x,i) ? (i+=1) : break}
|
114
|
+
return i
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
# Concatenate the items into a string.
|
119
|
+
#
|
120
|
+
# ==Example
|
121
|
+
# arr=['anne','beth','cate']
|
122
|
+
# arr.cat => "annebethcate"
|
123
|
+
#
|
124
|
+
# You can optionally provide a prefix and suffix.
|
125
|
+
#
|
126
|
+
# ==Example
|
127
|
+
# arr.cat("+") => "+anne+beth+cate"
|
128
|
+
# arr.cat("+","-") => "+anne-+beth-+cate-"
|
129
|
+
#
|
130
|
+
# You can easily wrap items in HTML tags.
|
131
|
+
#
|
132
|
+
# ==Example
|
133
|
+
# arr=['anne','beth','cate']
|
134
|
+
# arr.cat("<li>","</li>\n")
|
135
|
+
# =>
|
136
|
+
# <li>anne</li>
|
137
|
+
# <li>beth</li>
|
138
|
+
# <li>cate</li>
|
139
|
+
|
140
|
+
def cat(prefix=nil,suffix=nil)
|
141
|
+
if prefix
|
142
|
+
if suffix
|
143
|
+
inject(""){|sum,s| sum += prefix + s.to_s + suffix}
|
144
|
+
else
|
145
|
+
inject(""){|sum,s| sum += prefix + s.to_s }
|
146
|
+
end
|
147
|
+
else
|
148
|
+
inject(""){|sum,s| sum += s.to_s }
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'enumerable_extensions'
|
3
|
+
|
4
|
+
class EnumerableExtensionsTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
ITEMS = ['a','b','c']
|
7
|
+
|
8
|
+
|
9
|
+
########################################################################
|
10
|
+
#
|
11
|
+
# map
|
12
|
+
#
|
13
|
+
########################################################################
|
14
|
+
|
15
|
+
class Mock
|
16
|
+
attr_accessor :id
|
17
|
+
def initialize(id)
|
18
|
+
@id=id
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
A = Mock.new('a')
|
23
|
+
B = Mock.new('b')
|
24
|
+
C = Mock.new('c')
|
25
|
+
|
26
|
+
def test_map_id
|
27
|
+
assert_equal(['a','b','c'],[A,B,C].map_id)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_map_to_sym
|
31
|
+
assert_equal([:a,:b,:c],ITEMS.map_to_sym)
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
########################################################################
|
36
|
+
#
|
37
|
+
# select
|
38
|
+
#
|
39
|
+
########################################################################
|
40
|
+
|
41
|
+
|
42
|
+
def test_select_while
|
43
|
+
assert_equal([ ],ITEMS.select_while{|x| x < 'a' },'< a')
|
44
|
+
assert_equal(['a' ],ITEMS.select_while{|x| x < 'b' },'< b')
|
45
|
+
assert_equal(['a','b' ],ITEMS.select_while{|x| x < 'c' },'< c')
|
46
|
+
assert_equal(['a','b','c'],ITEMS.select_while{|x| x < 'd' },'< d')
|
47
|
+
assert_equal(['a','b','c'],ITEMS.select_while{|x| x < 'e' },'< e')
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_select_until_condition
|
51
|
+
assert_equal([ ],ITEMS.select_until{|x| x == 'a' },'a')
|
52
|
+
assert_equal(['a' ],ITEMS.select_until{|x| x == 'b' },'b')
|
53
|
+
assert_equal(['a','b' ],ITEMS.select_until{|x| x == 'c' },'c')
|
54
|
+
assert_equal(['a','b','c'],ITEMS.select_until{|x| x == 'd' },'d')
|
55
|
+
assert_equal(['a','b','c'],ITEMS.select_until{|x| x == 'e' },'e')
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_select_with_index
|
59
|
+
assert_equal([ ],ITEMS.select_with_index{|x,i| i < 0 },'i < 0')
|
60
|
+
assert_equal(['a' ],ITEMS.select_with_index{|x,i| i < 1 },'i < 1')
|
61
|
+
assert_equal(['a','b' ],ITEMS.select_with_index{|x,i| i < 2 },'i < 2')
|
62
|
+
assert_equal(['a','b','c'],ITEMS.select_with_index{|x,i| i < 3 },'i < 3')
|
63
|
+
assert_equal(['a','b','c'],ITEMS.select_with_index{|x,i| i < 4 },'i < 4')
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
########################################################################
|
68
|
+
#
|
69
|
+
# nitems
|
70
|
+
#
|
71
|
+
########################################################################
|
72
|
+
|
73
|
+
|
74
|
+
def test_nitems_while
|
75
|
+
assert_equal(0,ITEMS.nitems_while{|x| x < 'a' },'< a')
|
76
|
+
assert_equal(1,ITEMS.nitems_while{|x| x < 'b' },'< b')
|
77
|
+
assert_equal(2,ITEMS.nitems_while{|x| x < 'c' },'< c')
|
78
|
+
assert_equal(3,ITEMS.nitems_while{|x| x < 'd' },'< d')
|
79
|
+
assert_equal(3,ITEMS.nitems_while{|x| x < 'e' },'< e')
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_nitems_until
|
83
|
+
assert_equal(0,ITEMS.nitems_until{|x| x == 'a' },'a')
|
84
|
+
assert_equal(1,ITEMS.nitems_until{|x| x == 'b' },'b')
|
85
|
+
assert_equal(2,ITEMS.nitems_until{|x| x == 'c' },'c')
|
86
|
+
assert_equal(3,ITEMS.nitems_until{|x| x == 'd' },'d')
|
87
|
+
assert_equal(3,ITEMS.nitems_until{|x| x == 'e' },'e')
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_nitems_with_index
|
91
|
+
assert_equal(0,ITEMS.nitems_with_index{|x,i| i < 0 },'i < 0')
|
92
|
+
assert_equal(1,ITEMS.nitems_with_index{|x,i| i < 1 },'i < 1')
|
93
|
+
assert_equal(2,ITEMS.nitems_with_index{|x,i| i < 2 },'i < 2')
|
94
|
+
assert_equal(3,ITEMS.nitems_with_index{|x,i| i < 3 },'i < 3')
|
95
|
+
assert_equal(3,ITEMS.nitems_with_index{|x,i| i < 4 },'i < 4')
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
########################################################################
|
100
|
+
#
|
101
|
+
# strings
|
102
|
+
#
|
103
|
+
########################################################################
|
104
|
+
|
105
|
+
|
106
|
+
def test_cat
|
107
|
+
a=['anne','beth','cate']
|
108
|
+
assert_equal("annebethcate",a.cat,"cat()")
|
109
|
+
assert_equal("+anne+beth+cate",a.cat('+'),"cat('+')")
|
110
|
+
assert_equal("+anne-+beth-+cate-",a.cat("+","-"),"cat('+','-')")
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webget-enumerable_extend
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- WebGet
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: webget@webget.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/enumerable_extensions.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage: http://webget.com/
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: "0"
|
38
|
+
version:
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.2.0
|
49
|
+
signing_key:
|
50
|
+
specification_version: 2
|
51
|
+
summary: Ruby Enumerable base class extensions
|
52
|
+
test_files:
|
53
|
+
- test/unit/enumerable_extensions_test.rb
|