hash-polyfill 0.1.20170629 → 0.1.20180120
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +13 -5
- data/lib/hash-polyfill.rb +17 -0
- data/spec/hash_polyfill_spec.rb +24 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1c972a4f79be7c3c78811cecf259ebf6e6be96e92c7812df42326b770c57a9a0
|
4
|
+
data.tar.gz: a6cb3ec4847b0edc0b25ba339e48d6719ee018255d8d508e48e1e9fc1c5bbaf4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30b9e9207e4b3de05e53564bcf149548811f3d25ca0dcdb340b1f0b741a707de75ed151fa24e7717e1226f24c591d6185e774a6a92f7c29ba0b5a32783699ff9
|
7
|
+
data.tar.gz: 42e13d1390fed6a6ad16e89f132899b02e43dcb4c27014ec0f3472c246ad155c6a6f375927fa28c91937e94b028e9ed17e3ef5a9d6434069e176cfaa7dfdce89
|
data/README.md
CHANGED
@@ -1,18 +1,26 @@
|
|
1
1
|
Implements some `Hash` methods which ruby will undoubtedly add in the future, possibly with different names.
|
2
2
|
|
3
|
+
To install run `gem install hash-polyfill`, and then `require "hash-polyfill"` in your code.
|
4
|
+
|
3
5
|
## Usage
|
4
6
|
|
5
7
|
```ruby
|
6
|
-
a = {a: 1, b: 2, c: 3, d:
|
8
|
+
a = {a: 1, b: 2, c: 3, d: 4}
|
7
9
|
|
8
|
-
a.select_values(
|
9
|
-
a.reject_values(
|
10
|
+
a.select_values(&:even?) # => {b: 2, d: 4}
|
11
|
+
a.reject_values(&:even?) # => {a: 1, c: 3}
|
10
12
|
a.transform_values{|v| v * 10} # => {a: 10, b: 20, c: 30, d: 40}
|
13
|
+
a.slice(:a, :b, :e) # => {a: 1, b: 2}
|
11
14
|
|
12
15
|
b = {a: 1, b: 2, c: nil, d: false}
|
13
16
|
b.compact # => {a: 1, b: 2, d: false}
|
14
17
|
```
|
15
18
|
|
16
|
-
The gem only adds the methods if they don't already exist.
|
19
|
+
The gem only adds the methods if they don't already exist.
|
20
|
+
|
21
|
+
* In ruby 2.4+, `Hash#transform_values` is already provided.
|
22
|
+
* In ruby 2.4+, `Hash#transform_values!` is already provided.
|
23
|
+
* In ruby 2.5+, `Hash#transform_keys` is already provided.
|
24
|
+
* In ruby 2.5+, `Hash#slice` is already provided.
|
17
25
|
|
18
|
-
There are also `!` versions of
|
26
|
+
There are also `!` versions of some of these methods.
|
data/lib/hash-polyfill.rb
CHANGED
@@ -55,4 +55,21 @@ class Hash
|
|
55
55
|
end
|
56
56
|
self
|
57
57
|
end unless method_defined? :transform_values!
|
58
|
+
|
59
|
+
def slice(*values)
|
60
|
+
result = {}
|
61
|
+
values.each do |key|
|
62
|
+
result[key] = self[key] if has_key?(key)
|
63
|
+
end
|
64
|
+
result
|
65
|
+
end unless method_defined? :slice
|
66
|
+
|
67
|
+
def transform_keys
|
68
|
+
return enum_for(:transform_keys) unless block_given?
|
69
|
+
result = {}
|
70
|
+
each do |key, value|
|
71
|
+
result[yield(key)] = value
|
72
|
+
end
|
73
|
+
result
|
74
|
+
end unless method_defined? :transform_keys
|
58
75
|
end
|
data/spec/hash_polyfill_spec.rb
CHANGED
@@ -113,4 +113,28 @@ describe "Hash polyfill" do
|
|
113
113
|
expect(a.transform_values!.each(&:itself)).to eq(a)
|
114
114
|
expect(a).to eq({a: 3, b: 4, c: 5, d: 6})
|
115
115
|
end
|
116
|
+
|
117
|
+
# Ruby 2.5 polyfill
|
118
|
+
it "slice" do
|
119
|
+
a = {a: 2, b: 3, c: 4, d: 5}
|
120
|
+
b = a.slice(:a, :b, :e)
|
121
|
+
expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
|
122
|
+
expect(b).to eq({a: 2, b: 3})
|
123
|
+
end
|
124
|
+
|
125
|
+
# Ruby 2.5 polyfill
|
126
|
+
it "transform_keys - with block" do
|
127
|
+
a = {a: 2, b: 3, c: 4, d: 5}
|
128
|
+
b = a.transform_keys(&:next)
|
129
|
+
expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
|
130
|
+
expect(b).to eq({b: 2, c: 3, d: 4, e: 5})
|
131
|
+
end
|
132
|
+
|
133
|
+
it "transform_keys - no block yields Enumerator" do
|
134
|
+
a = {a: 2, b: 3, c: 4, d: 5}
|
135
|
+
expect(a.transform_keys).to be_instance_of(Enumerator)
|
136
|
+
b = a.transform_keys.each(&:next)
|
137
|
+
expect(a).to eq({a: 2, b: 3, c: 4, d: 5})
|
138
|
+
expect(b).to eq({b: 2, c: 3, d: 4, e: 5})
|
139
|
+
end
|
116
140
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-polyfill
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.20180120
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomasz Wegrzanowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: Hash
|
55
|
+
description: New Hash methods from 2.4/2.5 and well as Hash convenience methods from
|
56
56
|
the future
|
57
57
|
email: Tomasz.Wegrzanowski@gmail.com
|
58
58
|
executables: []
|
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
85
|
version: '0'
|
86
86
|
requirements: []
|
87
87
|
rubyforge_project:
|
88
|
-
rubygems_version: 2.
|
88
|
+
rubygems_version: 2.7.3
|
89
89
|
signing_key:
|
90
90
|
specification_version: 4
|
91
91
|
summary: Polyfill for Hash methods ruby will get in the future
|