Sha256: abd105dc74a676d683e39815bdc6a9c472bdf3ab2c139257bb7e78d493e6163d

Contents?: true

Size: 1.45 KB

Versions: 115

Compression:

Stored size: 1.45 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 loop together select.
  #
  # together_select has aliases [:tselect, :together_find_all, :tfindall]
  #
  # if you want to single Array return
  #   firsts = [1, 2, 3, 4]
  #   seconds =  [4, 2, 3, 1]
  #   ret = [firsts, seconds].together_select{|first, second|first == second}
  #   print ret # => output  [[2, 3], [2, 3]]
  #
  # if you want to multi Array return
  #   firsts = [1, 2, 3, 4]
  #   seconds =  [4, 2, 3, 1]
  #   ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
  #   print ret # => output  [[1, 3], [4, 2]]
  def together_select
    if_not_contain_array_rails_type_error
    ret = []
    first.each_with_index do |i_v, i|
      eval_each_str = get_args_str_for_together i
      each_ret = instance_eval "yield(#{eval_each_str})"
      ret = set_together_each_return_select(ret, each_ret, i)
    end
    ret
  end

  private

  def set_together_each_return_select(ret, each_ret, index)
    unless together_return_multi?(each_ret)
      tmp_each_ret = []
      size.times { tmp_each_ret << each_ret }
      each_ret = tmp_each_ret
    end
    size.times { |i|ret << [] } if index == 0
    (0..(size - 1)).each { |i|ret[i] << self[i][index] if each_ret[i] }
    ret
  end

  alias_methods [:together_find_all, :tselect, :tfindall], :together_select
end

Version data entries

115 entries across 115 versions & 1 rubygems

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