lib/open_classes/array.rb in tbpgr_utils-0.0.34 vs lib/open_classes/array.rb in tbpgr_utils-0.0.35
- old
+ new
@@ -531,11 +531,10 @@
def together_insert(index, *args)
if_not_contain_array_rails_type_error
each { |list|list.insert(index, *args) }
end
-
# Arrays bulk shift.
#
# together_shift has alias :tshift
#
# not empty case
@@ -568,10 +567,46 @@
else
reduce([]) { |ret, list|ret << list.shift(count) }
end
end
+ # Arrays bulk pop.
+ #
+ # together_pop has alias :tpop
+ #
+ # not empty case
+ # lists = [[1, 2], [5, 6]]
+ # ret = lists.together_pop
+ # print ret # => [2, 6]
+ # print lists # => [1, 5]
+ #
+ # empty case
+ # lists = [[], []]
+ # ret = lists.together_pop
+ # print ret # => [nil, nil]
+ # print lists # => [[], []]
+ #
+ # not empty case with args
+ # lists = [[1, 2], [5, 6]]
+ # ret = lists.together_pop 2
+ # print ret # => [[1, 2], [5, 6]]
+ # print lists # => [[], []]
+ #
+ # not empty case with args
+ # lists = [[], []]
+ # ret = lists.together_pop 2
+ # print ret # => [[], []]
+ # print lists # => [[], []]
+ def together_pop(count = nil)
+ if_not_contain_array_rails_type_error
+ if count.nil?
+ reduce([]) { |ret, list|ret << list.pop }
+ else
+ reduce([]) { |ret, list|ret << list.pop(count) }
+ end
+ 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
@@ -631,9 +666,10 @@
alias_method :tlast, :together_last
alias_method :tinclude?, :together_include?
alias_method :tindex, :together_index
alias_method :tinsert, :together_insert
alias_method :tshift, :together_shift
+ alias_method :tpop, :together_pop
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