with_last 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/README.md +34 -8
- data/lib/with_last/core_ext.rb +20 -7
- data/lib/with_last/version.rb +1 -1
- data/with_last.gemspec +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 685c2bbb6dc0932893fabad565230bec602f31a6842a2bdd77b5ff2acce3579e
|
4
|
+
data.tar.gz: 10f1062f9ac05b8b53077a424f5e6b0aea012fbefbcff7b52167e0a33e107565
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be50db532602bf55613740211ed8361b17bd7978e9c30e256c2ca8d3152b128efce35271cc7f38ebda32520f2bd771025875cfff96ffff1dc399ba82c9876602
|
7
|
+
data.tar.gz: 64997c4794f6d2ca09594abffcf6d5085b5e0ee3f9b9cf4c29671171a61daeea724339f62f3f1f852b207035493e5e67fb9f80d8fc445c4eb505fa3df1c5db26
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
|
4
4
|
# with_last.rb
|
5
5
|
|
6
|
-
Add `with_last` method to `Enumerator` class
|
6
|
+
- Add `with_last` method to `Enumerator` class.
|
7
|
+
- Add `last?` method to `Enumerator` class.
|
8
|
+
- Add `each_with_last` to `Enumerable` module.
|
7
9
|
|
8
10
|
## Installation
|
9
11
|
|
@@ -17,27 +19,51 @@ And then execute:
|
|
17
19
|
|
18
20
|
## Usage
|
19
21
|
|
22
|
+
### `Enumerable#each_with_last`
|
23
|
+
|
20
24
|
```ruby
|
21
25
|
[1,2,3].each_with_last { |item, last|
|
22
|
-
print item
|
23
|
-
print last ? 'done!' : '->'
|
26
|
+
print "#{item}#{last ? '!' : ' => '}"
|
24
27
|
}
|
28
|
+
```
|
29
|
+
|
30
|
+
it prints `1 => 2 => 3!`
|
31
|
+
|
32
|
+
### `Enumerator#with_last`
|
33
|
+
|
34
|
+
```ruby
|
25
35
|
|
26
|
-
|
36
|
+
[1,2,3]
|
37
|
+
.map
|
38
|
+
.with_last { |item, last| "#{item * item}#{last ? '.' : ''}" }
|
39
|
+
.join(',')
|
40
|
+
```
|
41
|
+
|
42
|
+
it returns `"1,4,9."`
|
27
43
|
|
28
|
-
|
44
|
+
### `Enumerator#last?`
|
29
45
|
|
30
|
-
|
46
|
+
```ruby
|
47
|
+
e = [1,2].to_enum
|
48
|
+
e.last? # => false
|
49
|
+
e.next # => 1
|
50
|
+
e.last? # => false
|
51
|
+
e.next # => 2
|
52
|
+
e.last? # => true
|
31
53
|
```
|
32
54
|
|
55
|
+
### in ERB
|
56
|
+
|
33
57
|
```erb
|
34
58
|
<% %w[hoge fuga piyo].each_with_last do |item, is_last| %>
|
35
59
|
<%= item %><%= is_last ? '.' : ', ' %>
|
36
60
|
<% end %>
|
61
|
+
```
|
62
|
+
|
63
|
+
it renders;
|
37
64
|
|
38
|
-
|
65
|
+
```html
|
39
66
|
hoge, fuga, piyo.
|
40
|
-
-->
|
41
67
|
```
|
42
68
|
|
43
69
|
## License
|
data/lib/with_last/core_ext.rb
CHANGED
@@ -7,24 +7,37 @@ class Enumerator
|
|
7
7
|
# [1,2].each.with_last { |item, is_last| puts [item, is_last] }
|
8
8
|
# # => [1, false] [2, true]
|
9
9
|
#
|
10
|
+
#
|
10
11
|
# [1,2].map.with_last { |item, is_last| "#{item}#{is_last ? '.' : ', '}" }.join
|
11
12
|
# # => "1, 2."
|
12
13
|
#
|
14
|
+
#
|
13
15
|
# %w[hoge fuga].map.with_index.with_last { |item, index, is_last| "#{index}: #{item}#{is_last ? '.' : ', '}" }.join
|
14
16
|
# # => "0:hoge, 1:fuga."
|
15
17
|
def with_last
|
16
18
|
return to_enum :with_last unless block_given?
|
17
19
|
|
18
20
|
each do |*args|
|
19
|
-
|
20
|
-
|
21
|
-
peek
|
22
|
-
yield(*args, false)
|
23
|
-
rescue StopIteration => _e
|
24
|
-
yield(*args, true)
|
25
|
-
end
|
21
|
+
self.next
|
22
|
+
yield(*args, last?)
|
26
23
|
end
|
27
24
|
end
|
25
|
+
|
26
|
+
# Returns whether the enumerator is last.
|
27
|
+
#
|
28
|
+
# e = [1,2].to_enum
|
29
|
+
# e.last? # => false
|
30
|
+
# e.next # => 1
|
31
|
+
# e.last? # => false
|
32
|
+
# e.next # => 2
|
33
|
+
# e.last? # => true
|
34
|
+
#
|
35
|
+
def last?
|
36
|
+
peek
|
37
|
+
false
|
38
|
+
rescue StopIteration => _e
|
39
|
+
true
|
40
|
+
end
|
28
41
|
end
|
29
42
|
|
30
43
|
# open core class
|
data/lib/with_last/version.rb
CHANGED
data/with_last.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.metadata['homepage_uri'] = spec.homepage
|
18
18
|
spec.metadata['source_code_uri'] = 'https://github.com/oieioi/with_last.rb'
|
19
19
|
spec.metadata['changelog_uri'] = 'https://github.com/oieioi/with_last.rb/releases'
|
20
|
+
spec.metadata['documentation_uri'] = 'https://www.rubydoc.info/gems/with_last'
|
20
21
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
22
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: with_last
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- oieioi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Add `with_last` to Enumerator and `each_with_last` to Enumerable
|
14
14
|
email:
|
@@ -39,6 +39,7 @@ metadata:
|
|
39
39
|
homepage_uri: https://github.com/oieioi/with_last.rb
|
40
40
|
source_code_uri: https://github.com/oieioi/with_last.rb
|
41
41
|
changelog_uri: https://github.com/oieioi/with_last.rb/releases
|
42
|
+
documentation_uri: https://www.rubydoc.info/gems/with_last
|
42
43
|
post_install_message:
|
43
44
|
rdoc_options: []
|
44
45
|
require_paths:
|
@@ -54,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
55
|
- !ruby/object:Gem::Version
|
55
56
|
version: '0'
|
56
57
|
requirements: []
|
57
|
-
rubygems_version: 3.
|
58
|
+
rubygems_version: 3.0.3
|
58
59
|
signing_key:
|
59
60
|
specification_version: 4
|
60
61
|
summary: Add `with_last` to Enumerator and `each_with_last` to Enumerable
|