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.151 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.150 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.149 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.148 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.147 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.146 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.145 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.144 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.143 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.142 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.141 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.140 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.139 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.138 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.137 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.136 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.135 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.134 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.133 lib/open_classes/array/together_fill.rb
tbpgr_utils-0.0.132 lib/open_classes/array/together_fill.rb