lib/open_classes/array.rb in tbpgr_utils-0.0.24 vs lib/open_classes/array.rb in tbpgr_utils-0.0.25
- old
+ new
@@ -277,10 +277,42 @@
each { |list|ret << list.delete(value) }
default_return = block_given? ? yield : nil
ret.compact.size == 0 ? default_return : value
end
+ # Arrays bulk delete_at.
+ #
+ # together_delete_at has alias :tdelete_at
+ #
+ # if delete_at target is exist
+ # child1 = [1, 2, 3, 4]
+ # child2 = [2, 3, 4, 5]
+ # lists = [child1, child2]
+ # ret = lists.together_delete_at 2
+ # print ret # => [3, 4]
+ # print lists # => output [[1, 2, 4], [2, 3, 5]]
+ #
+ # if delete_at target is not exist
+ # child1 = [1, 2, 3, 4]
+ # child2 = [2, 3, 4, 5]
+ # lists = [child1, child2]
+ # ret = lists.together_delete_at 6
+ # print ret # => [nil, nil]
+ # print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
+ #
+ # if delete_at target is exist(minus index)
+ # child1 = [1, 2, 3, 4]
+ # child2 = [2, 3, 4, 5]
+ # lists = [child1, child2]
+ # ret = lists.together_delete_at -3
+ # print ret # => [2, 3]
+ # print lists # => output [[1, 3, 4], [2, 4, 5]]
+ def together_delete_at(index)
+ if_not_contain_array_rails_type_error
+ reduce([]) { |ret, list|ret << list.delete_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
@@ -340,9 +372,10 @@
alias_method :tat, :together_at
alias_method :tclear, :together_clear
alias_method :tcompact, :together_compact
alias_method :tcompact!, :together_compact!
alias_method :tdelete, :together_delete
+ alias_method :tdelete_at, :together_delete_at
alias_methods [:together_collect, :tmap, :tcollect], :together_map
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