rb_ext 0.1.0 → 0.2.0
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/VERSION +1 -1
- data/lib/rb_ext/array.rb +85 -0
- data/rb_ext.gemspec +61 -0
- metadata +4 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/rb_ext/array.rb
CHANGED
@@ -5,12 +5,14 @@ module RbExt
|
|
5
5
|
#
|
6
6
|
# -- TODO: modify to support nested arrays
|
7
7
|
#
|
8
|
+
# Example:
|
8
9
|
#
|
9
10
|
# a = %w(a b c - d e f g - h i j - k)
|
10
11
|
# a.split("-") # => [[a, b, c], [d, e, f, g], [h, i, j] [k]]
|
11
12
|
#
|
12
13
|
# a = ["a", "b", ["c", "d", "-", "e"], "-", "f"]
|
13
14
|
# a.split("-") # => [["a", "b", ["c", "d", "-", "e"]], ["f"]]
|
15
|
+
#
|
14
16
|
def split(separator)
|
15
17
|
if self.count(separator) >= 1
|
16
18
|
sep_indices = []
|
@@ -37,10 +39,13 @@ module RbExt
|
|
37
39
|
# Computes each element of the array with _n_
|
38
40
|
# and supports nested arrays.
|
39
41
|
#
|
42
|
+
# Example:
|
43
|
+
#
|
40
44
|
# a = ["a", "b", 1, "c", 2.5, ["d", 3], "e"]
|
41
45
|
# a.n_up(2) # => ["aa", "bb", 2, "cc", 5.0, ["dd", 6], "ee"]
|
42
46
|
#
|
43
47
|
# a3.n_up(3.5) # => ["aaa", "bbb", 3.5, "ccc", 8.75, ["ddd", 10.5], "eee"]
|
48
|
+
#
|
44
49
|
def n_up(n)
|
45
50
|
result = []
|
46
51
|
|
@@ -54,5 +59,85 @@ module RbExt
|
|
54
59
|
|
55
60
|
result
|
56
61
|
end
|
62
|
+
|
63
|
+
# Flattens first level of an array.
|
64
|
+
#
|
65
|
+
# Example:
|
66
|
+
#
|
67
|
+
# [1, [2, [3]]].flatten_once # => [1, 2, [3]]
|
68
|
+
#
|
69
|
+
def flatten_top
|
70
|
+
self.inject([]) do |acc, item|
|
71
|
+
if item.is_a?(Array)
|
72
|
+
acc.concat(item)
|
73
|
+
else
|
74
|
+
acc << item
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns a hash where its keys are the string versions
|
80
|
+
# of the values in the array and invokes a block for each
|
81
|
+
# element in the array taking the return value of the block
|
82
|
+
# as the value for the hash element.
|
83
|
+
#
|
84
|
+
# Example:
|
85
|
+
#
|
86
|
+
# ["a", "b", "c"].to_hash_keys { |arr, e| arr.index(e) } # => {"a"=>0, "b"=>1, "c"=>2}
|
87
|
+
# ["a", "b", "c", ["d", "e"]].to_hash_keys { |arr, e| arr.index(e) } # => {"a"=>0, "b"=>1, "c"=>2, "[\"d\", \"e\"]"=>{"d"=>0, "e"=>1}}
|
88
|
+
#
|
89
|
+
def to_hash_keys(&block)
|
90
|
+
out = {}
|
91
|
+
|
92
|
+
self.collect do |item|
|
93
|
+
if item.respond_to?(:to_hash_keys)
|
94
|
+
out[item.to_s] = item.to_hash_keys(&block)
|
95
|
+
else
|
96
|
+
out[item.to_s] = block.call(self, item)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
out
|
101
|
+
end
|
102
|
+
|
103
|
+
# Returns a hash where its values are the string versions
|
104
|
+
# of the values in the array and invokes a block for each
|
105
|
+
# element in the array taking the return value of the block
|
106
|
+
# as the key for the hash element.
|
107
|
+
#
|
108
|
+
# Example:
|
109
|
+
#
|
110
|
+
# ["a", "b", "c"].to_hash_values { |arr, e| arr.index(e) } # => {"0"=>"a", "1"=>"b", "2"=>"c"}
|
111
|
+
# ["a", "b", "c", ["d", "e"]].to_hash_values { |arr, e| arr.index(e) } # => {"0"=>"a", "1"=>"b", "2"=>"c", "{\"0\"=>\"d\", \"1\"=>\"e\"}"=>["d", "e"]}
|
112
|
+
#
|
113
|
+
def to_hash_values(&block)
|
114
|
+
out = {}
|
115
|
+
|
116
|
+
self.collect do |item|
|
117
|
+
if item.respond_to?(:to_hash_values)
|
118
|
+
out[item.to_hash_values(&block).to_s] = item
|
119
|
+
else
|
120
|
+
out[block.call(self, item).to_s] = item
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
out
|
125
|
+
end
|
126
|
+
|
127
|
+
# Combines the array and the given array (or string)
|
128
|
+
# using the values of this array as keys and the value
|
129
|
+
# of the given array as values and returns all together
|
130
|
+
# as a single hash.
|
131
|
+
#
|
132
|
+
# Example:
|
133
|
+
#
|
134
|
+
# %w(a b c).to_hash( %w(1 2 3) ) # => {"a"=>"1", "b"=>"2", "c"=>"3"}
|
135
|
+
# ["a", ["b", "c"]].to_hash( %w(1 2 3) ) # => {"a"=>"1", ["b", "c"]=>"2"}
|
136
|
+
# ["1", "2", "3"].to_hash(["a", ["b", "c"]]) # => {"1"=>"a", "2"=>["b", "c"], "3"=>nil}
|
137
|
+
# %w(a b c d).to_hash("hugo") # => {"a"=>"h", "b"=>"u", "c"=>"g", "d"=>"o"}
|
138
|
+
#
|
139
|
+
def to_hash(values)
|
140
|
+
Hash[ *(0...self.size()).inject([]) { |arr, ix| arr.push(self[ix], values[ix]) } ]
|
141
|
+
end
|
57
142
|
end
|
58
143
|
end
|
data/rb_ext.gemspec
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rb_ext}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["icatcher.at"]
|
12
|
+
s.date = %q{2011-04-25}
|
13
|
+
s.description = %q{Adds a bunch of methods to Ruby's Core Library, like Array#split or Array#double}
|
14
|
+
s.email = %q{kontakt@icatcher.at}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/rb_ext.rb",
|
27
|
+
"lib/rb_ext/array.rb",
|
28
|
+
"rb_ext.gemspec",
|
29
|
+
"test/helper.rb",
|
30
|
+
"test/test_rb_ext_array.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/icatcher-at/rb_ext}
|
33
|
+
s.licenses = ["MIT"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.7}
|
36
|
+
s.summary = %q{Adds a bunch of methods to Ruby's Core Library}
|
37
|
+
s.test_files = [
|
38
|
+
"test/helper.rb",
|
39
|
+
"test/test_rb_ext_array.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
49
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
53
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
58
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- icatcher.at
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- VERSION
|
79
79
|
- lib/rb_ext.rb
|
80
80
|
- lib/rb_ext/array.rb
|
81
|
+
- rb_ext.gemspec
|
81
82
|
- test/helper.rb
|
82
83
|
- test/test_rb_ext_array.rb
|
83
84
|
has_rdoc: true
|
@@ -94,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
95
|
requirements:
|
95
96
|
- - ">="
|
96
97
|
- !ruby/object:Gem::Version
|
97
|
-
hash:
|
98
|
+
hash: -695607921
|
98
99
|
segments:
|
99
100
|
- 0
|
100
101
|
version: "0"
|