with_last 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aacef2632f84cec36d0a06dd1fb1dbb2eaa8a944fef4114223f66b454b975e1d
4
- data.tar.gz: 45c8b3edd27a8d6d63669b3ce20a908b962a0f898dd9261cbe7eba3b9614219f
3
+ metadata.gz: 685c2bbb6dc0932893fabad565230bec602f31a6842a2bdd77b5ff2acce3579e
4
+ data.tar.gz: 10f1062f9ac05b8b53077a424f5e6b0aea012fbefbcff7b52167e0a33e107565
5
5
  SHA512:
6
- metadata.gz: 176e1a03ba751cbc4b989cc77898a7e171baa3d9ecdf5b0581fb62f14490ed1c426114774088699ab1ea2c1c025fcc00cd6618b011c67be91864ffd72417d676
7
- data.tar.gz: ebcd453a6a21b88a668bc5feb53d5f838d2d70c71e1931e6e5ea697da3475aded45fc3e19a6ac66b3aa84a68d27a496707a7b624cd93278047ae7ba49a5d40d6
6
+ metadata.gz: be50db532602bf55613740211ed8361b17bd7978e9c30e256c2ca8d3152b128efce35271cc7f38ebda32520f2bd771025875cfff96ffff1dc399ba82c9876602
7
+ data.tar.gz: 64997c4794f6d2ca09594abffcf6d5085b5e0ee3f9b9cf4c29671171a61daeea724339f62f3f1f852b207035493e5e67fb9f80d8fc445c4eb505fa3df1c5db26
data/.rubocop.yml CHANGED
@@ -16,3 +16,7 @@ Style/HashTransformKeys:
16
16
  Style/HashTransformValues:
17
17
  Enabled: true
18
18
 
19
+ Metrics/BlockLength:
20
+ Exclude:
21
+ - 'spec/**/*_spec.rb'
22
+
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 and `each_with_last` to `Enumerable` module.
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
- # "1->2->3done!"
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
- [1,2,3].map.with_last { |item, last| "#{item * item}#{last ? '.' : ''}" }.join(',')
44
+ ### `Enumerator#last?`
29
45
 
30
- # => "1,4,9."
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
@@ -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
- begin
20
- self.next
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WithLast
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
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.2.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-02-28 00:00:00.000000000 Z
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.1.2
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