rb_ext 0.2.0 → 0.3.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/LICENSE.txt +1 -1
- data/README.rdoc +4 -27
- data/VERSION +1 -1
- data/lib/rb_ext/hash.rb +53 -0
- data/lib/rb_ext.rb +3 -1
- data/rb_ext.gemspec +3 -2
- metadata +5 -4
data/LICENSE.txt
CHANGED
data/README.rdoc
CHANGED
@@ -12,35 +12,12 @@ or simply add the dependency to the Gemfile of your application:
|
|
12
12
|
|
13
13
|
and then call bundle install on your console.
|
14
14
|
|
15
|
-
|
15
|
+
== Extensions
|
16
16
|
|
17
|
-
|
17
|
+
* {Array}[https://github.com/icatcher-at/rb_ext/wiki/Array]
|
18
|
+
* {Hash}[https://github.com/icatcher-at/rb_ext/wiki/Hash]
|
18
19
|
|
19
|
-
=== Array#split(_separator_)
|
20
|
-
|
21
|
-
Splits an array by _separator_ and returns an array holding
|
22
|
-
the splitted values in arrays. Works like String#split for arrays but
|
23
|
-
only on the first level of the array (for now).
|
24
|
-
|
25
|
-
Example:
|
26
|
-
a = %w(a b c - d e f g - h i j - k)
|
27
|
-
a.split("-") # => [[a, b, c], [d, e, f, g], [h, i, j] [k]]
|
28
|
-
|
29
|
-
a = ["a", "b", ["c", "d", "-", "e"], "-", "f"]
|
30
|
-
a.split("-") # => [["a", "b", ["c", "d", "-", "e"]], ["f"]]
|
31
|
-
|
32
|
-
=== Array#n_up(_n_)
|
33
|
-
|
34
|
-
Computes each element of the array with _n_. Supports nested arrays.
|
35
|
-
|
36
|
-
Example:
|
37
|
-
a = ["a", "b", 1, "c", 2.5, ["d", 3], "e"]
|
38
|
-
a.n_up(2) # => ["aa", "bb", 2, "cc", 5.0, ["dd", 6], "ee"]
|
39
|
-
|
40
|
-
a3.n_up(3.5) # => ["aaa", "bbb", 3.5, "ccc", 8.75, ["ddd", 10.5], "eee"]
|
41
20
|
|
42
21
|
== Copyright
|
43
22
|
|
44
|
-
Copyright (c) 2011 Robert Neumayr. See LICENSE.txt for
|
45
|
-
further details.
|
46
|
-
|
23
|
+
Copyright (c) 2011 Robert Neumayr. See LICENSE.txt for further details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/rb_ext/hash.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module RbExt
|
2
|
+
module Hash
|
3
|
+
|
4
|
+
# Returns a copy of the hash containing only the key-value pairs
|
5
|
+
# matched by the key names given to the method.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# { :a => 1, :b => 2 }.pass(:a) # => { :a => 1 }
|
9
|
+
# { :abc => 1, :bbc => 2 }.pass(/^ab/) # => { :abc => 1 }
|
10
|
+
#
|
11
|
+
def only(*keys)
|
12
|
+
if keys.first.is_a? Regexp
|
13
|
+
reject { |key, value| key !~ keys.first }
|
14
|
+
else
|
15
|
+
reject { |key, value| !keys.include?(key) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns a copy of the hash containing only the key-value pairs
|
20
|
+
# that did not match the key names given to the method.
|
21
|
+
#
|
22
|
+
# Example:
|
23
|
+
# { :a => 1, :b => 2 }.block(:a) # => { :b => 2 }
|
24
|
+
# { :abc => 1, :bbc => 2 }.block(/^ab/) # => { :bbc => 2 }
|
25
|
+
#
|
26
|
+
def except(*keys)
|
27
|
+
if keys.first.is_a? Regexp
|
28
|
+
reject { |key, value| key =~ keys.first }
|
29
|
+
else
|
30
|
+
reject { |key, value| keys.include?(key) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns an array containing the values of the hash
|
35
|
+
# referenced by the keys given to the method.
|
36
|
+
#
|
37
|
+
# Example:
|
38
|
+
# { :a => 1, :b => 2, :c => 3 }.pick(:a, :c) # => [ 1, 3 ]
|
39
|
+
#
|
40
|
+
def pick(*keys)
|
41
|
+
return *keys.map { |key| self[key] } if keys.size > 1
|
42
|
+
self[keys.first]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Same return value as #pick but deletes the
|
46
|
+
# the keys from the hash also.
|
47
|
+
#
|
48
|
+
def pick!(*keys)
|
49
|
+
return *keys.map { |key| delete(key) } if keys.size > 1
|
50
|
+
delete(keys.first)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/rb_ext.rb
CHANGED
data/rb_ext.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rb_ext}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["icatcher.at"]
|
12
|
-
s.date = %q{2011-04-
|
12
|
+
s.date = %q{2011-04-27}
|
13
13
|
s.description = %q{Adds a bunch of methods to Ruby's Core Library, like Array#split or Array#double}
|
14
14
|
s.email = %q{kontakt@icatcher.at}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"lib/rb_ext.rb",
|
27
27
|
"lib/rb_ext/array.rb",
|
28
|
+
"lib/rb_ext/hash.rb",
|
28
29
|
"rb_ext.gemspec",
|
29
30
|
"test/helper.rb",
|
30
31
|
"test/test_rb_ext_array.rb"
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- icatcher.at
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-04-
|
17
|
+
date: 2011-04-27 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- VERSION
|
79
79
|
- lib/rb_ext.rb
|
80
80
|
- lib/rb_ext/array.rb
|
81
|
+
- lib/rb_ext/hash.rb
|
81
82
|
- rb_ext.gemspec
|
82
83
|
- test/helper.rb
|
83
84
|
- test/test_rb_ext_array.rb
|
@@ -95,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
96
|
requirements:
|
96
97
|
- - ">="
|
97
98
|
- !ruby/object:Gem::Version
|
98
|
-
hash: -
|
99
|
+
hash: -701913363
|
99
100
|
segments:
|
100
101
|
- 0
|
101
102
|
version: "0"
|