hash-utils 0.14.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +3 -5
- data/README.md +5 -5
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/hash-utils.gemspec +13 -14
- data/lib/hash-utils/array.rb +84 -7
- data/lib/hash-utils/hash.rb +156 -22
- data/lib/hash-utils/object.rb +22 -0
- data/lib/hash-utils/string.rb +133 -6
- data/lib/hash-utils/symbol.rb +15 -1
- data/test +109 -1
- metadata +9 -12
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -6,12 +6,10 @@ GEM
|
|
6
6
|
bundler (~> 1.0.0)
|
7
7
|
git (>= 1.2.5)
|
8
8
|
rake
|
9
|
-
rake (0.
|
10
|
-
riot (0.12.
|
9
|
+
rake (0.9.0)
|
10
|
+
riot (0.12.3)
|
11
11
|
rr
|
12
|
-
term-ansicolor
|
13
12
|
rr (1.0.2)
|
14
|
-
term-ansicolor (1.0.5)
|
15
13
|
|
16
14
|
PLATFORMS
|
17
15
|
ruby
|
@@ -19,4 +17,4 @@ PLATFORMS
|
|
19
17
|
DEPENDENCIES
|
20
18
|
bundler (~> 1.0.0)
|
21
19
|
jeweler (~> 1.5.2)
|
22
|
-
riot (>= 0.12.
|
20
|
+
riot (>= 0.12.3)
|
data/README.md
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
Hash Utils
|
2
2
|
==========
|
3
3
|
|
4
|
-
**hash-utils** adds more than
|
4
|
+
**hash-utils** adds more than 95 useful and frequently rather
|
5
5
|
fundamental methods which are missing in Ruby programming language,
|
6
6
|
to several core classes. It tries to be similar project to
|
7
7
|
[Ruby Facets][1] on principle, but less complex, more practical,
|
8
8
|
non-atomic and organized by better way.
|
9
9
|
|
10
|
-
- `Array` –
|
10
|
+
- `Array` – 10 methods,
|
11
11
|
- `File` – 2 methods,
|
12
|
-
- `Hash` –
|
12
|
+
- `Hash` – 36 methods,
|
13
13
|
- `IO` – 1 method,
|
14
14
|
- `Module` – 1 method,
|
15
|
-
- `Object` –
|
16
|
-
- `String` –
|
15
|
+
- `Object` – 6 methods,
|
16
|
+
- `String` – 34 methods,
|
17
17
|
- `StringIO` – 1 method,
|
18
18
|
- `Symbol` – 7 methods.
|
19
19
|
|
data/Rakefile
CHANGED
@@ -16,8 +16,8 @@ Jeweler::Tasks.new do |gem|
|
|
16
16
|
gem.name = "hash-utils"
|
17
17
|
gem.homepage = "http://github.com/martinkozak/hash-utils"
|
18
18
|
gem.license = "MIT"
|
19
|
-
gem.summary = 'Adds more than
|
20
|
-
gem.post_install_message = "\nHASH UTILS: Be warn, Hash#all?
|
19
|
+
gem.summary = 'Adds more than 95 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.'
|
20
|
+
gem.post_install_message = "\nHASH UTILS: Be warn, Hash#all? has been removed since version 0.15.0 because of\nconflict with built-in one with in fact equivalent functionallity. Please, check\nyour code if you can and switch to Ruby's one.\n\n"
|
21
21
|
gem.email = "martinkozak@martinkozak.net"
|
22
22
|
gem.authors = ["Martin Kozák"]
|
23
23
|
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.15.0
|
data/hash-utils.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{hash-utils}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.15.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date = %q{2011-
|
11
|
+
s.authors = [%q{Martin Kozák}]
|
12
|
+
s.date = %q{2011-05-29}
|
13
13
|
s.email = %q{martinkozak@martinkozak.net}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -39,17 +39,16 @@ Gem::Specification.new do |s|
|
|
39
39
|
"test"
|
40
40
|
]
|
41
41
|
s.homepage = %q{http://github.com/martinkozak/hash-utils}
|
42
|
-
s.licenses = [
|
42
|
+
s.licenses = [%q{MIT}]
|
43
43
|
s.post_install_message = %q{
|
44
|
-
HASH UTILS: Be warn, Hash#all?
|
45
|
-
conflict with built-in one with in fact equivalent functionallity.
|
46
|
-
|
47
|
-
to Ruby's one.
|
44
|
+
HASH UTILS: Be warn, Hash#all? has been removed since version 0.15.0 because of
|
45
|
+
conflict with built-in one with in fact equivalent functionallity. Please, check
|
46
|
+
your code if you can and switch to Ruby's one.
|
48
47
|
|
49
48
|
}
|
50
|
-
s.require_paths = [
|
51
|
-
s.rubygems_version = %q{1.
|
52
|
-
s.summary = %q{Adds more than
|
49
|
+
s.require_paths = [%q{lib}]
|
50
|
+
s.rubygems_version = %q{1.8.4}
|
51
|
+
s.summary = %q{Adds more than 95 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.}
|
53
52
|
|
54
53
|
if s.respond_to? :specification_version then
|
55
54
|
s.specification_version = 3
|
@@ -57,16 +56,16 @@ to Ruby's one.
|
|
57
56
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
58
57
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
58
|
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
60
|
-
s.add_development_dependency(%q<riot>, [">= 0.12.
|
59
|
+
s.add_development_dependency(%q<riot>, [">= 0.12.3"])
|
61
60
|
else
|
62
61
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
62
|
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
64
|
-
s.add_dependency(%q<riot>, [">= 0.12.
|
63
|
+
s.add_dependency(%q<riot>, [">= 0.12.3"])
|
65
64
|
end
|
66
65
|
else
|
67
66
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
68
67
|
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
69
|
-
s.add_dependency(%q<riot>, [">= 0.12.
|
68
|
+
s.add_dependency(%q<riot>, [">= 0.12.3"])
|
70
69
|
end
|
71
70
|
end
|
72
71
|
|
data/lib/hash-utils/array.rb
CHANGED
@@ -53,7 +53,7 @@ class Array
|
|
53
53
|
# Should be noted, it cannot be named +#to_hash+, because #to_hash
|
54
54
|
# is called by the +Hash#[]+ itself. Reasons why it's absolutely
|
55
55
|
# undocumented call are unknown.
|
56
|
-
|
56
|
+
#
|
57
57
|
# @example Equivalent calls
|
58
58
|
# [["aa", "bb"], ["bb", "aa"]].to_h
|
59
59
|
# ["aa", "bb", "bb", "aa"].to_h(:flat)
|
@@ -65,11 +65,11 @@ class Array
|
|
65
65
|
#
|
66
66
|
|
67
67
|
def to_h(mode = nil)
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
68
|
+
if mode == :flat
|
69
|
+
Hash[*self]
|
70
|
+
else
|
71
|
+
Hash[self]
|
72
|
+
end
|
73
73
|
end
|
74
74
|
|
75
75
|
##
|
@@ -89,9 +89,86 @@ class Array
|
|
89
89
|
#
|
90
90
|
|
91
91
|
def merge!(*arrays)
|
92
|
-
arrays.flatten!
|
92
|
+
arrays.flatten! 1
|
93
93
|
arrays.each { |i| self << i }
|
94
94
|
self
|
95
95
|
end
|
96
96
|
|
97
|
+
##
|
98
|
+
# Returns the second item in the array.
|
99
|
+
#
|
100
|
+
# @return [Object] item of the array
|
101
|
+
# @since 0.15.0
|
102
|
+
#
|
103
|
+
|
104
|
+
def second
|
105
|
+
self[1]
|
106
|
+
end
|
107
|
+
|
108
|
+
##
|
109
|
+
# Returns the third item in the array.
|
110
|
+
#
|
111
|
+
# @return [Object] item of the array
|
112
|
+
# @since 0.15.0
|
113
|
+
#
|
114
|
+
|
115
|
+
def third
|
116
|
+
self[2]
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# Returns the fourth item in the array.
|
121
|
+
#
|
122
|
+
# @return [Object] item of the array
|
123
|
+
# @since 0.15.0
|
124
|
+
#
|
125
|
+
|
126
|
+
def fourth
|
127
|
+
self[3]
|
128
|
+
end
|
129
|
+
|
130
|
+
##
|
131
|
+
# Returns the fifth item in the array.
|
132
|
+
#
|
133
|
+
# @return [Object] item of the array
|
134
|
+
# @since 0.15.0
|
135
|
+
#
|
136
|
+
|
137
|
+
def fifth
|
138
|
+
self[4]
|
139
|
+
end
|
140
|
+
|
141
|
+
##
|
142
|
+
# Returns the sixth item in the array.
|
143
|
+
#
|
144
|
+
# @return [Object] item of the array
|
145
|
+
# @since 0.15.0
|
146
|
+
#
|
147
|
+
|
148
|
+
def sixth
|
149
|
+
self[5]
|
150
|
+
end
|
151
|
+
|
152
|
+
##
|
153
|
+
# Returns the seventh item in the array.
|
154
|
+
#
|
155
|
+
# @return [Object] item of the array
|
156
|
+
# @since 0.15.0
|
157
|
+
#
|
158
|
+
|
159
|
+
def seventh
|
160
|
+
self[6]
|
161
|
+
end
|
162
|
+
|
163
|
+
##
|
164
|
+
# Returns the eighth item in the array.
|
165
|
+
#
|
166
|
+
# @return [Object] item of the array
|
167
|
+
# @since 0.15.0
|
168
|
+
#
|
169
|
+
|
170
|
+
def eighth
|
171
|
+
self[7]
|
172
|
+
end
|
173
|
+
|
97
174
|
end
|
data/lib/hash-utils/hash.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
+
require "hash-utils/array"
|
5
|
+
|
4
6
|
##
|
5
7
|
# Hash extension.
|
6
8
|
#
|
@@ -219,14 +221,21 @@ class Hash
|
|
219
221
|
alias :collect_values! :map_values!
|
220
222
|
|
221
223
|
##
|
222
|
-
# Converts all keys to symbols.
|
224
|
+
# Converts all keys to symbols. Since version +0.14.1+ converts
|
225
|
+
# only +String+ keys to symbols.
|
223
226
|
#
|
224
227
|
# @return [Hash] new hash
|
225
228
|
# @since 0.1.0
|
226
229
|
#
|
227
230
|
|
228
231
|
def keys_to_sym
|
229
|
-
self.map_keys
|
232
|
+
self.map_keys do |k|
|
233
|
+
if k.kind_of? String
|
234
|
+
k.to_sym
|
235
|
+
else
|
236
|
+
k
|
237
|
+
end
|
238
|
+
end
|
230
239
|
end
|
231
240
|
|
232
241
|
##
|
@@ -257,24 +266,26 @@ class Hash
|
|
257
266
|
# @deprecated (since 0.10.0, conflict with built-in method)
|
258
267
|
# @since 0.2.0
|
259
268
|
#
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
269
|
+
#
|
270
|
+
# REMOVED SINCE 0.15.0
|
271
|
+
#
|
272
|
+
# def all?(&block)
|
273
|
+
# if block.arity == 2
|
274
|
+
# self.all_pairs? &block
|
275
|
+
# end
|
276
|
+
#
|
277
|
+
# if self.empty? or block.nil?
|
278
|
+
# return true
|
279
|
+
# end
|
280
|
+
#
|
281
|
+
# self.each_value do |v|
|
282
|
+
# if block.call(v) == false
|
283
|
+
# return false
|
284
|
+
# end
|
285
|
+
# end
|
286
|
+
#
|
287
|
+
# return true
|
288
|
+
# end
|
278
289
|
|
279
290
|
##
|
280
291
|
# Checks, all elements follow condition expressed in block.
|
@@ -336,7 +347,9 @@ class Hash
|
|
336
347
|
# @since 0.4.0
|
337
348
|
#
|
338
349
|
|
339
|
-
|
350
|
+
def to_h(mode = nil)
|
351
|
+
self
|
352
|
+
end
|
340
353
|
|
341
354
|
##
|
342
355
|
# Simulates sorting in place. In fact, replaces old one by new one.
|
@@ -532,7 +545,7 @@ class Hash
|
|
532
545
|
end
|
533
546
|
|
534
547
|
##
|
535
|
-
# Merges two hashes recursively and returns new
|
548
|
+
# Merges two hashes recursively and returns new +Hash+. Receives
|
536
549
|
# unlimited count of hashes for merging them in left to right order.
|
537
550
|
# Included hashes will be copied too.
|
538
551
|
#
|
@@ -560,4 +573,125 @@ class Hash
|
|
560
573
|
return result
|
561
574
|
end
|
562
575
|
|
576
|
+
##
|
577
|
+
# Iterates through items with given key only. None-existing values
|
578
|
+
# are ignored.
|
579
|
+
#
|
580
|
+
# @param [*Object] keys
|
581
|
+
# @yield [Array] pairs
|
582
|
+
# @since 0.15.0
|
583
|
+
#
|
584
|
+
|
585
|
+
def get_pairs(*args)
|
586
|
+
self.take_pairs(*args) do |i|
|
587
|
+
if not i.second.nil?
|
588
|
+
yield i
|
589
|
+
end
|
590
|
+
end
|
591
|
+
end
|
592
|
+
|
593
|
+
##
|
594
|
+
# Returns items from +Hash+ under given keys in required order.
|
595
|
+
# Doesn't keep default values settings. None-existing values are
|
596
|
+
# ignored.
|
597
|
+
#
|
598
|
+
# @example
|
599
|
+
# hash = {:a => 1, :b => 2, :c => 3}
|
600
|
+
# hash.get_items(:a, :c, :d) # will return {:a => 1, :c => 3}
|
601
|
+
#
|
602
|
+
# @param [*Object] keys
|
603
|
+
# @return [Hash] new hash
|
604
|
+
# @since 0.15.0
|
605
|
+
#
|
606
|
+
|
607
|
+
def get_items(*args)
|
608
|
+
result = { }
|
609
|
+
self.get_pairs(*args) do |key, value|
|
610
|
+
result[key] = value
|
611
|
+
end
|
612
|
+
|
613
|
+
return result
|
614
|
+
end
|
615
|
+
|
616
|
+
##
|
617
|
+
# Returns values from +Hash+ under given keys in required order.
|
618
|
+
#
|
619
|
+
# @example
|
620
|
+
# hash = {:a => 1, :b => 2, :c => 3}
|
621
|
+
# hash.get_values(:a, :c, :d) # will return [1, 3]
|
622
|
+
#
|
623
|
+
# @param [*Object] keys
|
624
|
+
# @return [Array] array of values
|
625
|
+
# @since 0.15.0
|
626
|
+
#
|
627
|
+
|
628
|
+
def get_values(*args)
|
629
|
+
result = [ ]
|
630
|
+
self.get_pairs(*args) do |key, value|
|
631
|
+
result << value
|
632
|
+
end
|
633
|
+
|
634
|
+
return result
|
635
|
+
end
|
636
|
+
|
637
|
+
##
|
638
|
+
# Iterates through items with given key only including none-existing
|
639
|
+
# values.
|
640
|
+
#
|
641
|
+
# @param [*Object] keys
|
642
|
+
# @yield [Array] pairs
|
643
|
+
# @since 0.15.0
|
644
|
+
#
|
645
|
+
|
646
|
+
def take_pairs(*args)
|
647
|
+
args.each do |i|
|
648
|
+
yield [i, self[i]]
|
649
|
+
end
|
650
|
+
end
|
651
|
+
|
652
|
+
##
|
653
|
+
# Returns items from +Hash+ under given keys in required order.
|
654
|
+
# Doesn't keep default values settings. Includes none-existing
|
655
|
+
# values.
|
656
|
+
#
|
657
|
+
# @example
|
658
|
+
# hash = {:a => 1, :b => 2, :c => 3}
|
659
|
+
# hash.take_items(:a, :c, :d) # will return {:a => 1, :c => 3, :d => nil}
|
660
|
+
#
|
661
|
+
# @param [*Object] keys
|
662
|
+
# @return [Hash] new hash
|
663
|
+
# @since 0.15.0
|
664
|
+
#
|
665
|
+
|
666
|
+
def take_items(*args)
|
667
|
+
result = { }
|
668
|
+
self.take_pairs(*args) do |key, value|
|
669
|
+
result[key] = value
|
670
|
+
end
|
671
|
+
|
672
|
+
return result
|
673
|
+
end
|
674
|
+
|
675
|
+
##
|
676
|
+
# Returns values from +Hash+ under given keys in required order
|
677
|
+
# including none-existing values.
|
678
|
+
#
|
679
|
+
# @example
|
680
|
+
# hash = {:a => 1, :b => 2, :c => 3}
|
681
|
+
# hash.take_values(:a, :c, :d) # will return [1, 3, nil]
|
682
|
+
#
|
683
|
+
# @param [*Object] keys
|
684
|
+
# @return [Array] array of values
|
685
|
+
# @since 0.15.0
|
686
|
+
#
|
687
|
+
|
688
|
+
def take_values(*args)
|
689
|
+
result = [ ]
|
690
|
+
self.take_pairs(*args) do |key, value|
|
691
|
+
result << value
|
692
|
+
end
|
693
|
+
|
694
|
+
return result
|
695
|
+
end
|
696
|
+
|
563
697
|
end
|
data/lib/hash-utils/object.rb
CHANGED
@@ -55,4 +55,26 @@ class Object
|
|
55
55
|
false
|
56
56
|
end
|
57
57
|
|
58
|
+
##
|
59
|
+
# Indicates, object is pure +true+, so +TrueClass+.
|
60
|
+
#
|
61
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
62
|
+
# @since 0.15.0
|
63
|
+
#
|
64
|
+
|
65
|
+
def true?
|
66
|
+
self.kind_of? TrueClass
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Indicates, object is pure +false+, so +FalseClass+.
|
71
|
+
#
|
72
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
73
|
+
# @since 0.15.0
|
74
|
+
#
|
75
|
+
|
76
|
+
def false?
|
77
|
+
self.kind_of? FalseClass
|
78
|
+
end
|
79
|
+
|
58
80
|
end
|
data/lib/hash-utils/string.rb
CHANGED
@@ -46,11 +46,11 @@ class String
|
|
46
46
|
# definitions. See {Array#to_h}.
|
47
47
|
#
|
48
48
|
# @example Equivalent calls
|
49
|
-
# "aa bb".
|
50
|
-
# "aa bb".
|
51
|
-
# "aa bb".
|
49
|
+
# "aa bb".strtr("aa" => "bb", "bb" => "aa")
|
50
|
+
# "aa bb".strtr([["aa", "bb"], ["bb", "aa"]])
|
51
|
+
# "aa bb".strtr(["aa", "bb", "bb", "aa"], :flat)
|
52
52
|
# @example Use with symbols
|
53
|
-
# "aa bb".
|
53
|
+
# "aa bb".strtr(:aa => "bb", :bb => "aa") { |s| s.to_sym }
|
54
54
|
#
|
55
55
|
# @param [Array, Hash] replacements replacements definition
|
56
56
|
# @param [Symbol] mode flat mode switch, can be +:flat+ or +nil+
|
@@ -190,7 +190,7 @@ class String
|
|
190
190
|
|
191
191
|
self
|
192
192
|
end
|
193
|
-
|
193
|
+
|
194
194
|
##
|
195
195
|
# Returns first character of the string.
|
196
196
|
#
|
@@ -202,7 +202,84 @@ class String
|
|
202
202
|
#
|
203
203
|
|
204
204
|
def first
|
205
|
-
self
|
205
|
+
self.chr
|
206
|
+
end
|
207
|
+
|
208
|
+
##
|
209
|
+
# Returns second character of the string.
|
210
|
+
#
|
211
|
+
# @return [String] second character
|
212
|
+
# @since 0.15.0
|
213
|
+
#
|
214
|
+
|
215
|
+
def second
|
216
|
+
self[1].chr
|
217
|
+
end
|
218
|
+
|
219
|
+
##
|
220
|
+
# Returns third character of the string.
|
221
|
+
#
|
222
|
+
# @return [String] third character
|
223
|
+
# @since 0.15.0
|
224
|
+
#
|
225
|
+
|
226
|
+
def third
|
227
|
+
self[2].chr
|
228
|
+
end
|
229
|
+
|
230
|
+
##
|
231
|
+
# Returns fourth character of the string.
|
232
|
+
#
|
233
|
+
# @return [String] fourth character
|
234
|
+
# @since 0.15.0
|
235
|
+
#
|
236
|
+
|
237
|
+
def fourth
|
238
|
+
self[3].chr
|
239
|
+
end
|
240
|
+
|
241
|
+
##
|
242
|
+
# Returns fifth character of the string.
|
243
|
+
#
|
244
|
+
# @return [String] fifth character
|
245
|
+
# @since 0.15.0
|
246
|
+
#
|
247
|
+
|
248
|
+
def fifth
|
249
|
+
self[4].chr
|
250
|
+
end
|
251
|
+
|
252
|
+
##
|
253
|
+
# Returns sixth character of the string.
|
254
|
+
#
|
255
|
+
# @return [String] sixth character
|
256
|
+
# @since 0.15.0
|
257
|
+
#
|
258
|
+
|
259
|
+
def sixth
|
260
|
+
self[5].chr
|
261
|
+
end
|
262
|
+
|
263
|
+
##
|
264
|
+
# Returns seventh character of the string.
|
265
|
+
#
|
266
|
+
# @return [String] seventh character
|
267
|
+
# @since 0.15.0
|
268
|
+
#
|
269
|
+
|
270
|
+
def seventh
|
271
|
+
self[6].chr
|
272
|
+
end
|
273
|
+
|
274
|
+
##
|
275
|
+
# Returns eighth character of the string.
|
276
|
+
#
|
277
|
+
# @return [String] eighth character
|
278
|
+
# @since 0.15.0
|
279
|
+
#
|
280
|
+
|
281
|
+
def eighth
|
282
|
+
self[7].chr
|
206
283
|
end
|
207
284
|
|
208
285
|
##
|
@@ -444,6 +521,56 @@ class String
|
|
444
521
|
alias :prepend :unshift
|
445
522
|
alias :append :<<
|
446
523
|
|
524
|
+
##
|
525
|
+
# Converts first character of the string to uppercase.
|
526
|
+
#
|
527
|
+
# @return [String] new string
|
528
|
+
# @see http://www.php.net/ucfirst
|
529
|
+
# @since 0.15.0
|
530
|
+
#
|
531
|
+
|
532
|
+
def ucfirst
|
533
|
+
self.dup.ucfirst!
|
534
|
+
end
|
535
|
+
|
536
|
+
##
|
537
|
+
# Converts first character of the string to uppercase in place.
|
538
|
+
#
|
539
|
+
# @return [String] new string
|
540
|
+
# @see http://www.php.net/ucfirst
|
541
|
+
# @since 0.15.0
|
542
|
+
#
|
543
|
+
|
544
|
+
def ucfirst!
|
545
|
+
self[0] = self.first.upcase
|
546
|
+
return self
|
547
|
+
end
|
548
|
+
|
549
|
+
##
|
550
|
+
# Converts first character of the string to lowercase.
|
551
|
+
#
|
552
|
+
# @return [String] new string
|
553
|
+
# @see http://www.php.net/lcfirst
|
554
|
+
# @since 0.15.0
|
555
|
+
#
|
556
|
+
|
557
|
+
def lcfirst
|
558
|
+
self.dup.lcfirst!
|
559
|
+
end
|
560
|
+
|
561
|
+
##
|
562
|
+
# Converts first character of the string to lowercase in place.
|
563
|
+
#
|
564
|
+
# @return [String] new string
|
565
|
+
# @see http://www.php.net/lcfirst
|
566
|
+
# @since 0.15.0
|
567
|
+
#
|
568
|
+
|
569
|
+
def lcfirst!
|
570
|
+
self[0] = self.first.downcase
|
571
|
+
return self
|
572
|
+
end
|
573
|
+
|
447
574
|
|
448
575
|
private
|
449
576
|
|
data/lib/hash-utils/symbol.rb
CHANGED
@@ -85,7 +85,7 @@ class Symbol
|
|
85
85
|
def append(string)
|
86
86
|
(self.to_s << string.to_s).to_sym
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
##
|
90
90
|
# Puts content to begin of symbol and returns new symbol.
|
91
91
|
#
|
@@ -97,5 +97,19 @@ class Symbol
|
|
97
97
|
def prepend(string)
|
98
98
|
(string.to_s + self.to_s).to_sym
|
99
99
|
end
|
100
|
+
|
101
|
+
##
|
102
|
+
# Splits symbol to more.
|
103
|
+
#
|
104
|
+
# @param [String] separator separator for splitting
|
105
|
+
# @param [Integer] count max new parts count
|
106
|
+
# @since 0.15.0
|
107
|
+
#
|
108
|
+
|
109
|
+
def split(separator = " ", count = 0)
|
110
|
+
self.to_s.split(separator, count).map! do |i|
|
111
|
+
i.to_sym
|
112
|
+
end
|
113
|
+
end
|
100
114
|
|
101
115
|
end
|
data/test
CHANGED
@@ -15,6 +15,30 @@ require "riot"
|
|
15
15
|
#
|
16
16
|
# doc. Jiří Souček
|
17
17
|
|
18
|
+
context "File" do
|
19
|
+
asserts("#second") do
|
20
|
+
[1, 2, 3, 4, 5, 6, 7, 8].second == 2
|
21
|
+
end
|
22
|
+
asserts("#third") do
|
23
|
+
[1, 2, 3, 4, 5, 6, 7, 8].third == 3
|
24
|
+
end
|
25
|
+
asserts("#fourth") do
|
26
|
+
[1, 2, 3, 4, 5, 6, 7, 8].fourth == 4
|
27
|
+
end
|
28
|
+
asserts("#fifth") do
|
29
|
+
[1, 2, 3, 4, 5, 6, 7, 8].fifth == 5
|
30
|
+
end
|
31
|
+
asserts("#sixth") do
|
32
|
+
[1, 2, 3, 4, 5, 6, 7, 8].sixth == 6
|
33
|
+
end
|
34
|
+
asserts("#seventh") do
|
35
|
+
[1, 2, 3, 4, 5, 6, 7, 8].seventh == 7
|
36
|
+
end
|
37
|
+
asserts("#eighth") do
|
38
|
+
[1, 2, 3, 4, 5, 6, 7, 8].eighth == 8
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
18
42
|
context "File" do
|
19
43
|
asserts("#touch") do
|
20
44
|
File.touch("./~test1")
|
@@ -48,6 +72,20 @@ context "Hash" do
|
|
48
72
|
t.compact!
|
49
73
|
t == { :a => 1 }
|
50
74
|
end
|
75
|
+
asserts("#get_pairs") do
|
76
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
77
|
+
result = [ ]
|
78
|
+
h.get_pairs(:a, :c, :d) { |i| result << i }
|
79
|
+
result == [[:a, 1], [:c, 3]]
|
80
|
+
end
|
81
|
+
asserts("#get_items") do
|
82
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
83
|
+
h.get_items(:a, :c, :d) == { :a => 1, :c => 3 }
|
84
|
+
end
|
85
|
+
asserts("#get_values") do
|
86
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
87
|
+
h.get_values(:a, :c, :d) == [1, 3]
|
88
|
+
end
|
51
89
|
asserts("#map_values") do
|
52
90
|
topic.map_values { |i| i + 1 } == { :a => 2, :b => 3 }
|
53
91
|
end
|
@@ -67,6 +105,30 @@ context "Hash" do
|
|
67
105
|
h.deep_merge!({:a => {:d => :f, :h => :i}})
|
68
106
|
h == {:a => {:b => :c, :d => :f, :h => :i}}
|
69
107
|
end
|
108
|
+
asserts("#keys_to_sym") do
|
109
|
+
h = {"a" => "b", 2 => "c", "d" => "e"}
|
110
|
+
h.keys_to_sym == {:a => "b", 2 => "c", :d => "e"}
|
111
|
+
end
|
112
|
+
asserts("#keys_to_sym!") do
|
113
|
+
h = {"a" => "b", 2 => "c", "d" => "e"}
|
114
|
+
h.keys_to_sym!
|
115
|
+
h == {:a => "b", 2 => "c", :d => "e"}
|
116
|
+
end
|
117
|
+
asserts("#take_pairs") do
|
118
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
119
|
+
result = [ ]
|
120
|
+
h.take_pairs(:a, :c, :d) { |i| result << i }
|
121
|
+
result == [[:a, 1], [:c, 3], [:d, nil]]
|
122
|
+
end
|
123
|
+
asserts("#take_items") do
|
124
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
125
|
+
h.take_items(:a, :c, :d) == { :a => 1, :c => 3, :d => nil }
|
126
|
+
end
|
127
|
+
asserts("#take_values") do
|
128
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
129
|
+
h.take_values(:a, :c, :d) == [1, 3, nil]
|
130
|
+
end
|
131
|
+
|
70
132
|
end
|
71
133
|
|
72
134
|
## IO
|
@@ -98,6 +160,9 @@ context "Object" do
|
|
98
160
|
asserts("#**") do
|
99
161
|
"ab" ** 5 == ["ab", "ab", "ab", "ab", "ab"]
|
100
162
|
end
|
163
|
+
asserts("#false?") do
|
164
|
+
(false.false? == true) and ("string".false? == false)
|
165
|
+
end
|
101
166
|
asserts("#in?") do
|
102
167
|
5.in? 1..7
|
103
168
|
end
|
@@ -107,13 +172,37 @@ context "Object" do
|
|
107
172
|
asserts("#to_b") do
|
108
173
|
(nil.to_b === false) and ("ab".to_b === true)
|
109
174
|
end
|
175
|
+
asserts("#true?") do
|
176
|
+
(true.true? == true) and ("string".true? == false)
|
177
|
+
end
|
110
178
|
end
|
111
179
|
|
112
180
|
## STRING
|
113
181
|
|
114
182
|
context "String" do
|
115
183
|
asserts("#first") do
|
116
|
-
"
|
184
|
+
"abcdefgh".first == ?a
|
185
|
+
end
|
186
|
+
asserts("#second") do
|
187
|
+
"abcdefgh".second == ?b
|
188
|
+
end
|
189
|
+
asserts("#third") do
|
190
|
+
"abcdefgh".third == ?c
|
191
|
+
end
|
192
|
+
asserts("#fourth") do
|
193
|
+
"abcdefgh".fourth == ?d
|
194
|
+
end
|
195
|
+
asserts("#fifth") do
|
196
|
+
"abcdefgh".fifth == ?e
|
197
|
+
end
|
198
|
+
asserts("#sixth") do
|
199
|
+
"abcdefgh".sixth == ?f
|
200
|
+
end
|
201
|
+
asserts("#seventh") do
|
202
|
+
"abcdefgh".seventh == ?g
|
203
|
+
end
|
204
|
+
asserts("#eighth") do
|
205
|
+
"abcdefgh".eighth == ?h
|
117
206
|
end
|
118
207
|
asserts("#first_line") do
|
119
208
|
res = true
|
@@ -139,6 +228,14 @@ context "String" do
|
|
139
228
|
res &= "a\nb".last_lines(2) == ["a\n", "b"]
|
140
229
|
res &= "a\nb\nc\n".last_lines(4) == ["a\n", "b\n", "c\n", ""]
|
141
230
|
end
|
231
|
+
asserts("#lcfirst") do
|
232
|
+
str = "ABCD"
|
233
|
+
str.lcfirst == "aBCD" and str == "ABCD"
|
234
|
+
end
|
235
|
+
asserts("#lcfirst!") do
|
236
|
+
str = "ABCD"
|
237
|
+
str.lcfirst! == "aBCD" and str == "aBCD"
|
238
|
+
end
|
142
239
|
asserts("#pop") do
|
143
240
|
res = true
|
144
241
|
str = "abcd"
|
@@ -193,6 +290,14 @@ context "String" do
|
|
193
290
|
res &= str.shift_lines(2) == ["a\n", "b\n"]
|
194
291
|
res &= str = "c\nd\n"
|
195
292
|
end
|
293
|
+
asserts("#ucfirst") do
|
294
|
+
str = "abcd"
|
295
|
+
str.ucfirst == "Abcd" and str == "abcd"
|
296
|
+
end
|
297
|
+
asserts("#ucfirst!") do
|
298
|
+
str = "abcd"
|
299
|
+
str.ucfirst! == "Abcd" and str == "Abcd"
|
300
|
+
end
|
196
301
|
asserts("#unshift") do
|
197
302
|
res = true
|
198
303
|
str = "abcd"
|
@@ -243,6 +348,9 @@ context "Symbol" do
|
|
243
348
|
asserts("#prepend") do
|
244
349
|
:abcd.prepend("012") == :"012abcd"
|
245
350
|
end
|
351
|
+
asserts("#split") do
|
352
|
+
:ab_cd_ef.split("_", 2) == [:ab, :cd_ef]
|
353
|
+
end
|
246
354
|
asserts("#start_with?") do
|
247
355
|
:abcde.start_with? "ghi", "abc"
|
248
356
|
end
|
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.15.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Martin Koz\xC3\xA1k"
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
14
|
-
default_executable:
|
13
|
+
date: 2011-05-29 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: bundler
|
@@ -42,7 +41,7 @@ dependencies:
|
|
42
41
|
requirements:
|
43
42
|
- - ">="
|
44
43
|
- !ruby/object:Gem::Version
|
45
|
-
version: 0.12.
|
44
|
+
version: 0.12.3
|
46
45
|
type: :development
|
47
46
|
prerelease: false
|
48
47
|
version_requirements: *id003
|
@@ -77,15 +76,13 @@ files:
|
|
77
76
|
- lib/hash-utils/stringio.rb
|
78
77
|
- lib/hash-utils/symbol.rb
|
79
78
|
- test
|
80
|
-
has_rdoc: true
|
81
79
|
homepage: http://github.com/martinkozak/hash-utils
|
82
80
|
licenses:
|
83
81
|
- MIT
|
84
82
|
post_install_message: "\n\
|
85
|
-
HASH UTILS: Be warn, Hash#all?
|
86
|
-
conflict with built-in one with in fact equivalent functionallity.
|
87
|
-
|
88
|
-
to Ruby's one.\n\n"
|
83
|
+
HASH UTILS: Be warn, Hash#all? has been removed since version 0.15.0 because of\n\
|
84
|
+
conflict with built-in one with in fact equivalent functionallity. Please, check\n\
|
85
|
+
your code if you can and switch to Ruby's one.\n\n"
|
89
86
|
rdoc_options: []
|
90
87
|
|
91
88
|
require_paths:
|
@@ -95,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
92
|
requirements:
|
96
93
|
- - ">="
|
97
94
|
- !ruby/object:Gem::Version
|
98
|
-
hash:
|
95
|
+
hash: 2809892554201068801
|
99
96
|
segments:
|
100
97
|
- 0
|
101
98
|
version: "0"
|
@@ -108,9 +105,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
105
|
requirements: []
|
109
106
|
|
110
107
|
rubyforge_project:
|
111
|
-
rubygems_version: 1.
|
108
|
+
rubygems_version: 1.8.4
|
112
109
|
signing_key:
|
113
110
|
specification_version: 3
|
114
|
-
summary: Adds more than
|
111
|
+
summary: Adds more than 95 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.
|
115
112
|
test_files: []
|
116
113
|
|