Sha256: 1136c3af570515be7831d11f8a5455cdce42c71f180006cc00203b5c1b2f54f1

Contents?: true

Size: 619 Bytes

Versions: 5

Compression:

Stored size: 619 Bytes

Contents

require "facets/enumerator"

class String

  # Returns an Enumerator for iterating over each
  # line of the string, stripped of whitespace on
  # either side.
  #
  def cleanlines(&block)
    if block
      scan(/^.*?$/) do |line|
        block.call(line.strip)
      end
    else
      Enumerator.new(self) do |output|
        scan(/^.*?$/) do |line|
          output.yield(line.strip)
        end
      end
    end
  end

end

=begin test

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

  a = []
  "a \n b \n c".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/cleanlines.rb
facets-2.8.3 lib/core/facets/string/cleanlines.rb
facets-2.8.2 lib/core/facets/string/cleanlines.rb
facets-2.8.1 lib/core/facets/string/cleanlines.rb
facets-2.8.0 lib/core/facets/string/cleanlines.rb