Sha256: b6f1d7a54b627ac581904ca6a7161da4b3340fc8eb6b2a456b140bc94852212a

Contents?: true

Size: 647 Bytes

Versions: 5

Compression:

Stored size: 647 Bytes

Contents

require "facets/enumerator"

class String

  # Returns an Enumerator for iterating over each
  # line of the string, void of the termining newline
  # character, in contrast to #lines which retains it.
  #
  def newlines(&block)
    if block
      scan(/^.*?$/) do |line|
        block.call(line.chomp)
      end
    else
      Enumerator.new(self) do |output|
        scan(/^.*?$/) do |line|
          output.yield(line.chomp)
        end
      end
    end
  end

end

=begin test

  "a\nb\nc".newlines.class.must == Enumerator
  "a\nb\nc".newlines.to_a.must == %w{a b c}

  a = []
  "a\nb\nc".newlines{|nl| a << nl}
  a.must == %w{a b c}

=end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/string/newlines.rb
facets-2.8.3 lib/core/facets/string/newlines.rb
facets-2.8.2 lib/core/facets/string/newlines.rb
facets-2.8.1 lib/core/facets/string/newlines.rb
facets-2.8.0 lib/core/facets/string/newlines.rb