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.71 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.70 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.69 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.68 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.67 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.66 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.65 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.64 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.63 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.62 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.61 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.60 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.59 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.58 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.57 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.56 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.55 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.54 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.53 lib/open_classes/array/together_select.rb
tbpgr_utils-0.0.52 lib/open_classes/array/together_select.rb