hash-utils 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/hash-utils.gemspec +58 -0
- data/lib/hash-utils.rb +1 -2
- data/lib/hash-utils/numeric.rb +1 -17
- data/lib/hash-utils/object.rb +12 -0
- data/lib/hash-utils/string.rb +64 -18
- data/lib/hash-utils/symbol.rb +1 -17
- data/test +6 -5
- metadata +5 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
data/hash-utils.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
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{hash-utils}
|
8
|
+
s.version = "0.8.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Martin Kozák"]
|
12
|
+
s.date = %q{2011-02-13}
|
13
|
+
s.email = %q{martinkozak@martinkozak.net}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE.txt",
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.md",
|
24
|
+
"Rakefile",
|
25
|
+
"TODO.txt",
|
26
|
+
"VERSION",
|
27
|
+
"hash-utils.gemspec",
|
28
|
+
"lib/hash-utils.rb",
|
29
|
+
"lib/hash-utils/array.rb",
|
30
|
+
"lib/hash-utils/hash.rb",
|
31
|
+
"lib/hash-utils/numeric.rb",
|
32
|
+
"lib/hash-utils/object.rb",
|
33
|
+
"lib/hash-utils/string.rb",
|
34
|
+
"lib/hash-utils/symbol.rb",
|
35
|
+
"test"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/martinkozak/hash-utils}
|
38
|
+
s.licenses = ["MIT"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.5.2}
|
41
|
+
s.summary = %q{Adds a lot of useful fundamental utility methods which are missing in Ruby, both to Array and Hash classes and introduces some useful methods and syntactic sugar to Numeric, Symbol and String classes too.}
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
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
|
+
else
|
50
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
51
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
55
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/lib/hash-utils.rb
CHANGED
data/lib/hash-utils/numeric.rb
CHANGED
@@ -1,20 +1,4 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
-
|
5
|
-
# Numeric extension.
|
6
|
-
#
|
7
|
-
|
8
|
-
class Numeric
|
9
|
-
##
|
10
|
-
# Indicates numeric is in some object which supports +#include?+.
|
11
|
-
#
|
12
|
-
# @param [Object] range container for analyzing
|
13
|
-
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
14
|
-
# @since 0.3.0
|
15
|
-
#
|
16
|
-
|
17
|
-
def in?(range)
|
18
|
-
range.include? self
|
19
|
-
end
|
20
|
-
end
|
4
|
+
require "hash-utils/object"
|
data/lib/hash-utils/object.rb
CHANGED
@@ -6,6 +6,18 @@
|
|
6
6
|
#
|
7
7
|
|
8
8
|
class Object
|
9
|
+
##
|
10
|
+
# Indicates object is in some object which supports +#include?+.
|
11
|
+
#
|
12
|
+
# @param [Object] range container for analyzing
|
13
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
14
|
+
# @since 0.8.0
|
15
|
+
#
|
16
|
+
|
17
|
+
def in?(range)
|
18
|
+
range.include? self
|
19
|
+
end
|
20
|
+
|
9
21
|
##
|
10
22
|
# Converts object to Boolean according to Ruby conversion rules.
|
11
23
|
#
|
data/lib/hash-utils/string.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
|
4
4
|
require "hash-utils/array"
|
5
5
|
require "hash-utils/hash"
|
6
|
+
require "hash-utils/object"
|
6
7
|
|
7
8
|
##
|
8
9
|
# String extension.
|
@@ -15,18 +16,6 @@ class String
|
|
15
16
|
|
16
17
|
NUMERIC = /^\s*-?\d+(?:\.\d+)?\s*$/
|
17
18
|
|
18
|
-
##
|
19
|
-
# Indicates string is in some object which supports +#include?+.
|
20
|
-
#
|
21
|
-
# @param [Object] range container for analyzing
|
22
|
-
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
23
|
-
# @since 0.3.0
|
24
|
-
#
|
25
|
-
|
26
|
-
def in?(range)
|
27
|
-
range.include? self
|
28
|
-
end
|
29
|
-
|
30
19
|
##
|
31
20
|
# Indicates, string is numeric, so consists of numbers only.
|
32
21
|
#
|
@@ -112,12 +101,7 @@ class String
|
|
112
101
|
#
|
113
102
|
|
114
103
|
def to_a
|
115
|
-
|
116
|
-
self.each_char do |ch|
|
117
|
-
output << ch
|
118
|
-
end
|
119
|
-
|
120
|
-
return output
|
104
|
+
self.split("")
|
121
105
|
end
|
122
106
|
|
123
107
|
##
|
@@ -158,6 +142,55 @@ class String
|
|
158
142
|
self
|
159
143
|
end
|
160
144
|
|
145
|
+
##
|
146
|
+
# Replaces all substrings defined by Regexp by complex way. It
|
147
|
+
# means, it gives to block not only whole match, but submatches too.
|
148
|
+
# In fact, emulates PHP's +preg_replace_callback()+ function.
|
149
|
+
# In other ways emulates standard +#gsub+.
|
150
|
+
#
|
151
|
+
# @param [Regexp] regexp matching expression
|
152
|
+
# @param [String] to new string
|
153
|
+
# @param [Proc] block block which will receive each match
|
154
|
+
# @return [String] resultant string
|
155
|
+
# @see http://www.php.net/preg_replace_callback
|
156
|
+
# @since 0.8.0
|
157
|
+
#
|
158
|
+
|
159
|
+
def gsub_f(from, to = nil, &block)
|
160
|
+
__prepare_gsub_f(from, to, block) do |callback|
|
161
|
+
if to.nil?
|
162
|
+
self.gsub(from, &callback)
|
163
|
+
else
|
164
|
+
self.gsub(from, to)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
##
|
170
|
+
# Performs complex regexp replacing same as {#gsub_f}, but in place.
|
171
|
+
# In other ways emulates standard +#gsub!+.
|
172
|
+
#
|
173
|
+
# @param [Regexp] from matching expression
|
174
|
+
# @param [String] to new string
|
175
|
+
# @param [Proc] block block which will receive each match
|
176
|
+
# @return [String] resultant string
|
177
|
+
# @see #gsub_f
|
178
|
+
# @since 0.8.0
|
179
|
+
#
|
180
|
+
|
181
|
+
def gsub_f!(from, to = nil, &block)
|
182
|
+
__prepare_gsub_f(from, to, block) do |callback|
|
183
|
+
if to.nil?
|
184
|
+
self.gsub!(from, &callback)
|
185
|
+
else
|
186
|
+
self.gsub!(from, to)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
self
|
191
|
+
end
|
192
|
+
|
193
|
+
|
161
194
|
|
162
195
|
private
|
163
196
|
|
@@ -174,5 +207,18 @@ class String
|
|
174
207
|
return [defs, matcher]
|
175
208
|
end
|
176
209
|
|
210
|
+
##
|
211
|
+
# Prepares #gsub_f family methods.
|
212
|
+
#
|
213
|
+
|
214
|
+
def __prepare_gsub_f(from, to = nil, callback = nil, &block)
|
215
|
+
if not callback.nil?
|
216
|
+
newcall = Proc::new do |s|
|
217
|
+
callback.call(s.match(from))
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
block.call(newcall)
|
222
|
+
end
|
177
223
|
end
|
178
224
|
|
data/lib/hash-utils/symbol.rb
CHANGED
@@ -1,20 +1,4 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
-
|
5
|
-
# Symbol extension.
|
6
|
-
#
|
7
|
-
|
8
|
-
class Symbol
|
9
|
-
##
|
10
|
-
# Indicates symbol is in some object which supports +#include?+.
|
11
|
-
#
|
12
|
-
# @param [Object] range container for analyzing
|
13
|
-
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
14
|
-
# @since 0.3.0
|
15
|
-
#
|
16
|
-
|
17
|
-
def in?(range)
|
18
|
-
range.include? self
|
19
|
-
end
|
20
|
-
end
|
4
|
+
require "hash-utils/object"
|
data/test
CHANGED
@@ -2,10 +2,11 @@
|
|
2
2
|
|
3
3
|
$:.push("./lib")
|
4
4
|
require "hash-utils/string"
|
5
|
-
foo = "
|
5
|
+
foo = "bb aa cc aa dd"
|
6
6
|
|
7
|
-
|
8
|
-
puts
|
7
|
+
result = foo.gsub_f(/..(aa)/) do |match|
|
8
|
+
puts match[1].inspect
|
9
|
+
"XX"
|
10
|
+
end
|
9
11
|
|
10
|
-
|
11
|
-
puts foo.inspect
|
12
|
+
puts result
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: hash-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.8.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Martin Koz\xC3\xA1k"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-02-
|
13
|
+
date: 2011-02-13 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- Rakefile
|
54
54
|
- TODO.txt
|
55
55
|
- VERSION
|
56
|
+
- hash-utils.gemspec
|
56
57
|
- lib/hash-utils.rb
|
57
58
|
- lib/hash-utils/array.rb
|
58
59
|
- lib/hash-utils/hash.rb
|
@@ -75,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
76
|
requirements:
|
76
77
|
- - ">="
|
77
78
|
- !ruby/object:Gem::Version
|
78
|
-
hash:
|
79
|
+
hash: 1134958876287378489
|
79
80
|
segments:
|
80
81
|
- 0
|
81
82
|
version: "0"
|
@@ -88,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
89
|
requirements: []
|
89
90
|
|
90
91
|
rubyforge_project:
|
91
|
-
rubygems_version: 1.5.
|
92
|
+
rubygems_version: 1.5.2
|
92
93
|
signing_key:
|
93
94
|
specification_version: 3
|
94
95
|
summary: Adds a lot of useful fundamental utility methods which are missing in Ruby, both to Array and Hash classes and introduces some useful methods and syntactic sugar to Numeric, Symbol and String classes too.
|