Sha256: a34a1632de361ff7d484cd9c1f4882450eab22a4f6e2fdf27029a3780497d388

Contents?: true

Size: 1.22 KB

Versions: 114

Compression:

Stored size: 1.22 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 sample.
  #
  # together_sample has alias :tsample
  #
  # not empty case
  #   lists = [[1, 2], [5, 6]]
  #   ret = lists.together_sample
  #   print ret # => [1 or 2, 5 or 6]
  #
  # empty case
  #   lists = [[], []]
  #   ret = lists.together_sample
  #   print ret # => [nil, nil]
  #
  # not empty case with args
  #   lists = [[1, 2], [5, 6]]
  #   ret = lists.together_sample 2
  #   print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]
  #
  # not empty case with args
  #   lists = [[], []]
  #   ret = lists.together_sample 2
  #   print ret # => [[], []]
  #
  # not empty, over size case with args
  #   lists = [[1, 2], [5, 6]]
  #   ret = lists.together_sample 3
  #   print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]
  #
  def together_sample(count = nil)
    if_not_contain_array_rails_type_error
    if count.nil?
      reduce([]) { |ret, list|ret << list.sample }
    else
      reduce([]) { |ret, list|ret << list.sample(count) }
    end
  end

  alias_method :tsample, :together_sample
end

Version data entries

114 entries across 114 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.150 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.149 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.148 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.147 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.146 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.145 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.144 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.143 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.142 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.141 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.140 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.139 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.138 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.137 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.136 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.135 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.134 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.133 lib/open_classes/array/together_sample.rb
tbpgr_utils-0.0.132 lib/open_classes/array/together_sample.rb