- Add
with_last
method toEnumerator
class. - Add
last?
method toEnumerator
class. - Add
each_with_last
toEnumerable
module.
gem 'with_last'
And then execute:
$ bundle install
[1,2,3].each_with_last { |item, last|
print "#{item}#{last ? '!' : ' => '}"
}
it prints 1 => 2 => 3!
[1,2,3]
.map
.with_last { |item, last| "#{item * item}#{last ? '.' : ''}" }
.join(',')
it returns "1,4,9."
e = [1,2].to_enum
e.last? # => false
e.next # => 1
e.last? # => false
e.next # => 2
e.last? # => true
<% %w[hoge fuga piyo].each_with_last do |item, is_last| %>
<%= item %><%= is_last ? '.' : ', ' %>
<% end %>
it renders;
hoge, fuga, piyo.
The gem is available as open source under the terms of the MIT License.