Sha256: bbabd359c84ec4067e04f8ed86a51d4bdf8ce16c08affe08b7279d0644ecc849

Contents?: true

Size: 1.46 KB

Versions: 11

Compression:

Stored size: 1.46 KB

Contents

module Ext
  module Array
    module ShiftWhile
      refine ::Array do
        # The algorithm ensures that the block sees the array as if the current
        # element has already been removed
        def shift_while(&block)
          r = []
          while value = self.shift
            if !block.call(value)
              self.unshift value
              break
            else
              r << value
            end
          end
          r
        end
      end
    end

    module PopWhile
      refine ::Array do
        # The algorithm ensures that the block sees the array as if the current
        # element has already been removed
        def pop_while(&block)
          r = []
          while value = self.pop
            if !block.call(value)
              self.push value
              break
            else
              r << value
            end
          end
          r
        end
      end
    end

    module Wrap
      refine ::Array do
        # Concatenate strings into lines that are at most +width+ characters wide
        def wrap(width, curr = 0)
          lines = [[]]
          curr -= 1 # Simplifies conditions below
          each { |word|
            if curr + 1 + word.size <= width
              lines.last << word
              curr += 1 + word.size
            else
              lines << [word]
              curr = word.size
            end
          }
          lines.map! { |words| words.join(" ") }
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
shellopts-2.0.10 lib/ext/array.rb
shellopts-2.0.9 lib/ext/array.rb
shellopts-2.0.8 lib/ext/array.rb
shellopts-2.0.7 lib/ext/array.rb
shellopts-2.0.6 lib/ext/array.rb
shellopts-2.0.5 lib/ext/array.rb
shellopts-2.0.4 lib/ext/array.rb
shellopts-2.0.3 lib/ext/array.rb
shellopts-2.0.2 lib/ext/array.rb
shellopts-2.0.1 lib/ext/array.rb
shellopts-2.0.0 lib/ext/array.rb