Sha256: 7941cfcc9a66d8bd3e86ae3488d9813877caf13236d65c3e4c6e030ed471fdda

Contents?: true

Size: 1.1 KB

Versions: 115

Compression:

Stored size: 1.1 KB

Contents

# encoding: utf-8
require 'open_classes/object'
require 'open_classes/module'
require 'open_classes/array/together_helper'

# Array
class Array
  include TogetherHelper

  # Arrays bulk fill.
  #
  # together_fill has alias :tfill
  #
  # not use block case
  #   lists = [[*1..5], [*6..10]]
  #   ret = lists.together_fill(99)
  #   print ret # => [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]]
  #
  # use block, no args case
  #   lists = [[*1..5], [*6..10]]
  #   ret = lists.together_fill { |i|(i + 1) + 1 }
  #   print ret # => [[2, 3, 4, 5, 6], [2, 3, 4, 5, 6]]
  #
  # use block, has args case
  #   lists = [[*1..5], [*6..10]]
  #   ret = lists.together_fill(2) { |i|(i + 1) + 1 }
  #   print ret # => [[1, 2, 4, 5, 6], [6, 7, 4, 5, 6]]
  def together_fill(fill_value = nil, &block)
    if_not_contain_array_rails_type_error
    if block
      fill_value = 0 if fill_value.nil?
      first.each_with_index do |i_v, i|
        next if i < fill_value
        each { |list|list[i] = yield(i) }
      end
    else
      each { |list|list.fill fill_value }
    end
    self
  end

  alias_method :tfill, :together_fill
end

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.131 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.130 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.129 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.128 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.127 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.126 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.125 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.124 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.123 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.122 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.121 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.120 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.119 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.118 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.117 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.116 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.115 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.114 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.113 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.112 lib/open_classes/array/together_fill.rb