lib/open_classes/array.rb in tbpgr_utils-0.0.19 vs lib/open_classes/array.rb in tbpgr_utils-0.0.20
- old
+ new
@@ -131,20 +131,40 @@
memo
end
# Arrays bulk concat.
#
+ # together_concat has alias :tconcat
+ #
# alpha = %w{one two three}
# numbers = %w{1 2 3}
# [alpha, numbers].together do |first, second|
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
# end
def together_concat(other)
if_not_contain_array_rails_type_error
each { |list|list.concat other }
end
+ # Arrays bulk at.
+ #
+ # together_at has alias :tat
+ #
+ # same elements size case
+ # alpha = %w{one two three}
+ # numbers = %w{1 2 3}
+ # [alpha, numbers].together_at 2 # => output ['three', 3]
+ #
+ # different elements size case
+ # alpha = %w{one two three}
+ # numbers = %w{1 2 nil}
+ # [alpha, numbers].together_at 2 # => output ['three', nil]
+ def together_at(index)
+ if_not_contain_array_rails_type_error
+ reduce([]) { |ats, list|ats << list.at(index) }
+ end
+
private
def if_not_contain_array_rails_type_error
each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array }
end
@@ -189,9 +209,10 @@
def together_return_multi?(list)
(list.class == Array && list.size == size).to_bool
end
alias_method :tconcat, :together_concat
+ alias_method :tat, :together_at
alias_methods [:together_collect, :tmap, :tcollect], :together_map
alias_methods [:together_find_all, :tselect, :tfindall], :together_select
alias_methods [:together_inject, :treduce, :tinject], :together_reduce
end