with_last 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe328cc0a626b1dba80f9002aec22e8b92d9ef38d0307f5b76faed27055d0e50
4
- data.tar.gz: 99b96dc991d95c7fd3d864b48b7a7bf5f55641b144d1255d7942e906a0178a83
3
+ metadata.gz: aacef2632f84cec36d0a06dd1fb1dbb2eaa8a944fef4114223f66b454b975e1d
4
+ data.tar.gz: 45c8b3edd27a8d6d63669b3ce20a908b962a0f898dd9261cbe7eba3b9614219f
5
5
  SHA512:
6
- metadata.gz: 203f0b954c84fed171203d09c1d297da52739a1fd46638113a1d4fdf0fb9e340863100a0e69b0da0a3e5def4e9139aeb7826fc1344dd8164ff17dfc8ebc10141
7
- data.tar.gz: 2a768b6156cfb5426a966237c6a7a942ac0134414f51791a7cb7246400994004272ce7f9d46fcfeb5d44dae2f0d116b86832f7c82cf080eb69eeb355eb45d635
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
@@ -11,3 +11,4 @@
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
13
  Gemfile.lock
14
+ .byebug_history
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
- source "https://rubygems.org"
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 "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
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
  [![Gem Version](https://badge.fury.io/rb/with_last.svg)](https://badge.fury.io/rb/with_last)
2
+ ![Ruby](https://github.com/oieioi/with_last.rb/workflows/Ruby/badge.svg)
2
3
 
3
4
  # with_last.rb
4
5
 
5
- Add `each_with_last` to `Array`.
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,4,5,6].each_with_last { |item, is_last|
21
- puts item
22
- puts is_last ? 'done' : 'to be continued'
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
- <% some_list.each_with_last do |item, is_last| %>
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
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "with_last"
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 "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
data/lib/with_last.rb CHANGED
@@ -1,14 +1,8 @@
1
- require "with_last/version"
1
+ # frozen_string_literal: true
2
2
 
3
- module WithLast
4
- class Error < StandardError; end
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
- def last?(index)
11
- size == (index + 1)
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WithLast
2
- VERSION = "0.1.1"
4
+ VERSION = '0.2.0'
3
5
  end
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 = "with_last"
6
+ spec.name = 'with_last'
5
7
  spec.version = WithLast::VERSION
6
- spec.authors = ["oieioi"]
7
- spec.email = ["atsuinatsu.samuifuyu@gmail.com"]
8
+ spec.authors = ['oieioi']
9
+ spec.email = ['atsuinatsu.samuifuyu@gmail.com']
8
10
 
9
- spec.summary = %q{Add `each_with_last` to Array}
10
- spec.description = %q{Add `each_with_last` to Array}
11
- spec.homepage = "https://github.com/oieioi/with_last.rb"
12
- spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
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["homepage_uri"] = spec.homepage
16
- spec.metadata["source_code_uri"] = "https://github.com/oieioi/with_last.rb"
17
- spec.metadata["changelog_uri"] = "https://github.com/oieioi/with_last.rb/releases"
18
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
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 = ["lib"]
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.1.1
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-27 00:00:00.000000000 Z
11
+ date: 2020-02-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Add `each_with_last` to Array
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
- - ".travis.yml"
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 Array
60
+ summary: Add `with_last` to Enumerator and `each_with_last` to Enumerable
59
61
  test_files: []
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.0
6
- before_install: gem install bundler -v 2.1.2