with_last 0.1.1 → 0.2.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 +4 -4
- data/.github/workflows/ruby.yml +37 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +18 -0
- data/Gemfile +8 -3
- data/README.md +17 -7
- data/Rakefile +5 -3
- data/bin/console +4 -3
- data/lib/with_last.rb +5 -11
- data/lib/with_last/core_ext.rb +41 -0
- data/lib/with_last/version.rb +3 -1
- data/with_last.gemspec +15 -13
- metadata +7 -5
- data/.travis.yml +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aacef2632f84cec36d0a06dd1fb1dbb2eaa8a944fef4114223f66b454b975e1d
|
4
|
+
data.tar.gz: 45c8b3edd27a8d6d63669b3ce20a908b962a0f898dd9261cbe7eba3b9614219f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 176e1a03ba751cbc4b989cc77898a7e171baa3d9ecdf5b0581fb62f14490ed1c426114774088699ab1ea2c1c025fcc00cd6618b011c67be91864ffd72417d676
|
7
|
+
data.tar.gz: ebcd453a6a21b88a668bc5feb53d5f838d2d70c71e1931e6e5ea697da3475aded45fc3e19a6ac66b3aa84a68d27a496707a7b624cd93278047ae7ba49a5d40d6
|
@@ -0,0 +1,37 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby 2.6
|
11
|
+
uses: actions/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 2.6.x
|
14
|
+
- name: Build
|
15
|
+
if: steps.cache.outputs.cache-hit != 'true'
|
16
|
+
run: |
|
17
|
+
gem install bundler
|
18
|
+
bundle config path vendor/bundle
|
19
|
+
bundle install --jobs 4 --retry 3
|
20
|
+
- name: Lint
|
21
|
+
run: bundle exec rubocop
|
22
|
+
lint:
|
23
|
+
runs-on: ubuntu-latest
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v2
|
26
|
+
- name: Set up Ruby 2.6
|
27
|
+
uses: actions/setup-ruby@v1
|
28
|
+
with:
|
29
|
+
ruby-version: 2.6.x
|
30
|
+
- name: Build
|
31
|
+
if: steps.cache.outputs.cache-hit != 'true'
|
32
|
+
run: |
|
33
|
+
gem install bundler
|
34
|
+
bundle config path vendor/bundle
|
35
|
+
bundle install --jobs 4 --retry 3
|
36
|
+
- name: Test
|
37
|
+
run: bundle exec rspec
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
|
4
|
+
Layout/LineLength:
|
5
|
+
# defualt: 80
|
6
|
+
Max: 160
|
7
|
+
IgnoredPatterns:
|
8
|
+
- '^\s*#'
|
9
|
+
|
10
|
+
Style/HashEachMethods:
|
11
|
+
Enabled: true
|
12
|
+
|
13
|
+
Style/HashTransformKeys:
|
14
|
+
Enabled: true
|
15
|
+
|
16
|
+
Style/HashTransformValues:
|
17
|
+
Enabled: true
|
18
|
+
|
data/Gemfile
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
2
4
|
|
3
5
|
# Specify your gem's dependencies in array_with_last.gemspec
|
4
6
|
gemspec
|
5
7
|
|
6
|
-
gem
|
7
|
-
gem
|
8
|
+
gem 'byebug'
|
9
|
+
gem 'rake', '~> 12.0'
|
10
|
+
gem 'rspec', '~> 3.0'
|
11
|
+
|
12
|
+
gem 'rubocop', '~> 0.80.0'
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
[](https://badge.fury.io/rb/with_last)
|
2
|
+

|
2
3
|
|
3
4
|
# with_last.rb
|
4
5
|
|
5
|
-
Add `each_with_last` to `
|
6
|
+
Add `with_last` method to `Enumerator` class and `each_with_last` to `Enumerable` module.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -17,17 +18,26 @@ And then execute:
|
|
17
18
|
## Usage
|
18
19
|
|
19
20
|
```ruby
|
20
|
-
[1,2,3
|
21
|
-
|
22
|
-
|
21
|
+
[1,2,3].each_with_last { |item, last|
|
22
|
+
print item
|
23
|
+
print last ? 'done!' : '->'
|
23
24
|
}
|
24
|
-
```
|
25
25
|
|
26
|
+
# "1->2->3done!"
|
27
|
+
|
28
|
+
[1,2,3].map.with_last { |item, last| "#{item * item}#{last ? '.' : ''}" }.join(',')
|
29
|
+
|
30
|
+
# => "1,4,9."
|
31
|
+
```
|
26
32
|
|
27
33
|
```erb
|
28
|
-
<%
|
29
|
-
<%= item %><%= is_last ? '.' : ',' %>
|
34
|
+
<% %w[hoge fuga piyo].each_with_last do |item, is_last| %>
|
35
|
+
<%= item %><%= is_last ? '.' : ', ' %>
|
30
36
|
<% end %>
|
37
|
+
|
38
|
+
<!--
|
39
|
+
hoge, fuga, piyo.
|
40
|
+
-->
|
31
41
|
```
|
32
42
|
|
33
43
|
## License
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'with_last'
|
5
6
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +11,5 @@ require "with_last"
|
|
10
11
|
# require "pry"
|
11
12
|
# Pry.start
|
12
13
|
|
13
|
-
require
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/lib/with_last.rb
CHANGED
@@ -1,14 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
class ::Array
|
6
|
-
def each_with_last
|
7
|
-
each_with_index { |item, index| yield(item, last?(index)) }
|
8
|
-
end
|
3
|
+
require 'with_last/version'
|
4
|
+
require 'with_last/core_ext'
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
end
|
6
|
+
# with_last namespace
|
7
|
+
module WithLast
|
14
8
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# open core class
|
4
|
+
class Enumerator
|
5
|
+
# Iterates with whether the item is the last item.
|
6
|
+
#
|
7
|
+
# [1,2].each.with_last { |item, is_last| puts [item, is_last] }
|
8
|
+
# # => [1, false] [2, true]
|
9
|
+
#
|
10
|
+
# [1,2].map.with_last { |item, is_last| "#{item}#{is_last ? '.' : ', '}" }.join
|
11
|
+
# # => "1, 2."
|
12
|
+
#
|
13
|
+
# %w[hoge fuga].map.with_index.with_last { |item, index, is_last| "#{index}: #{item}#{is_last ? '.' : ', '}" }.join
|
14
|
+
# # => "0:hoge, 1:fuga."
|
15
|
+
def with_last
|
16
|
+
return to_enum :with_last unless block_given?
|
17
|
+
|
18
|
+
each do |*args|
|
19
|
+
begin
|
20
|
+
self.next
|
21
|
+
peek
|
22
|
+
yield(*args, false)
|
23
|
+
rescue StopIteration => _e
|
24
|
+
yield(*args, true)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# open core class
|
31
|
+
module Enumerable
|
32
|
+
# Iterates with whether the item is the last item.
|
33
|
+
#
|
34
|
+
# [1,2].each_with_last { |item, is_last| puts [item, is_last] }
|
35
|
+
# # => [1, false] [2, true]
|
36
|
+
def each_with_last
|
37
|
+
return to_enum :each_with_last unless block_given?
|
38
|
+
|
39
|
+
each.with_last { |*args, last| yield(*args, last) }
|
40
|
+
end
|
41
|
+
end
|
data/lib/with_last/version.rb
CHANGED
data/with_last.gemspec
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'lib/with_last/version'
|
2
4
|
|
3
5
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name =
|
6
|
+
spec.name = 'with_last'
|
5
7
|
spec.version = WithLast::VERSION
|
6
|
-
spec.authors = [
|
7
|
-
spec.email = [
|
8
|
+
spec.authors = ['oieioi']
|
9
|
+
spec.email = ['atsuinatsu.samuifuyu@gmail.com']
|
8
10
|
|
9
|
-
spec.summary =
|
10
|
-
spec.description =
|
11
|
-
spec.homepage =
|
12
|
-
spec.license =
|
13
|
-
spec.required_ruby_version = Gem::Requirement.new(
|
11
|
+
spec.summary = 'Add `with_last` to Enumerator and `each_with_last` to Enumerable'
|
12
|
+
spec.description = 'Add `with_last` to Enumerator and `each_with_last` to Enumerable'
|
13
|
+
spec.homepage = 'https://github.com/oieioi/with_last.rb'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
14
16
|
|
15
|
-
spec.metadata[
|
16
|
-
spec.metadata[
|
17
|
-
spec.metadata[
|
18
|
-
spec.files
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/oieioi/with_last.rb'
|
19
|
+
spec.metadata['changelog_uri'] = 'https://github.com/oieioi/with_last.rb/releases'
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
21
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
22
|
end
|
21
|
-
spec.require_paths = [
|
23
|
+
spec.require_paths = ['lib']
|
22
24
|
end
|
metadata
CHANGED
@@ -1,25 +1,26 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: with_last
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2020-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Add `each_with_last` to
|
13
|
+
description: Add `with_last` to Enumerator and `each_with_last` to Enumerable
|
14
14
|
email:
|
15
15
|
- atsuinatsu.samuifuyu@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".github/workflows/ruby.yml"
|
20
21
|
- ".gitignore"
|
21
22
|
- ".rspec"
|
22
|
-
- ".
|
23
|
+
- ".rubocop.yml"
|
23
24
|
- CODE_OF_CONDUCT.md
|
24
25
|
- Gemfile
|
25
26
|
- LICENSE.txt
|
@@ -28,6 +29,7 @@ files:
|
|
28
29
|
- bin/console
|
29
30
|
- bin/setup
|
30
31
|
- lib/with_last.rb
|
32
|
+
- lib/with_last/core_ext.rb
|
31
33
|
- lib/with_last/version.rb
|
32
34
|
- with_last.gemspec
|
33
35
|
homepage: https://github.com/oieioi/with_last.rb
|
@@ -55,5 +57,5 @@ requirements: []
|
|
55
57
|
rubygems_version: 3.1.2
|
56
58
|
signing_key:
|
57
59
|
specification_version: 4
|
58
|
-
summary: Add `each_with_last` to
|
60
|
+
summary: Add `with_last` to Enumerator and `each_with_last` to Enumerable
|
59
61
|
test_files: []
|