Project

with_last

0.0
No commit activity in last 3 years
No release in over 3 years
Add `with_last` to Enumerator and `each_with_last` to Enumerable
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Gem Version Ruby

with_last.rb

  • Add with_last method to Enumerator class.
  • Add last? method to Enumerator class.
  • Add each_with_last to Enumerable module.

Installation

gem 'with_last'

And then execute:

$ bundle install

Usage

Enumerable#each_with_last

[1,2,3].each_with_last { |item, last|
  print "#{item}#{last ? '!' : ' => '}"
}

it prints 1 => 2 => 3!

Enumerator#with_last

[1,2,3]
  .map
  .with_last { |item, last| "#{item * item}#{last ? '.' : ''}" }
  .join(',')

it returns "1,4,9."

Enumerator#last?

e = [1,2].to_enum
e.last? # => false
e.next  # => 1
e.last? # => false
e.next  # => 2
e.last? # => true

in ERB

<% %w[hoge fuga piyo].each_with_last do |item, is_last| %>
  <%= item %><%= is_last ? '.' : ', ' %>
<% end %>

it renders;

hoge, fuga, piyo.

License

The gem is available as open source under the terms of the MIT License.