diff options
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 6096696cb..5201cbe57 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -126,6 +126,22 @@ class Array ## # call-seq: + # ary.difference(other_ary1, other_ary2, ...) -> new_ary + # + # Returns a new array that is a copy of the original array, removing all + # occurences of any item that also appear in +other_ary+. The order is + # preserved from the original array. + # + def difference(*args) + ary = self.dup + args.each do |x| + ary = self - x + end + ary + end + + ## + # call-seq: # ary & other_ary -> new_ary # # Set Intersection---Returns a new array @@ -266,7 +282,6 @@ class Array self end - NONE=Object.new ## # call-seq: # ary.fetch(index) -> obj @@ -816,12 +831,11 @@ class Array # a.permutation(0).to_a #=> [[]] # one permutation of length 0 # a.permutation(4).to_a #=> [] # no permutations of length 4 def permutation(n=self.size, &block) - size = self.size return to_enum(:permutation, n) unless block - return if n > size + size = self.size if n == 0 - yield [] - else + yield [] + elsif 0 < n && n <= size i = 0 while i<size result = [self[i]] @@ -836,6 +850,7 @@ class Array i += 1 end end + self end ## @@ -862,9 +877,8 @@ class Array # a.combination(5).to_a #=> [] # no combinations of length 5 def combination(n, &block) - size = self.size return to_enum(:combination, n) unless block - return if n > size + size = self.size if n == 0 yield [] elsif n == 1 @@ -873,7 +887,7 @@ class Array yield [self[i]] i += 1 end - else + elsif n <= size i = 0 while i<size result = [self[i]] @@ -883,6 +897,7 @@ class Array i += 1 end end + self end ## @@ -904,8 +919,8 @@ class Array column_count = nil self.each do |row| raise TypeError unless row.is_a?(Array) - column_count ||= row.count - raise IndexError, 'element size differs' unless column_count == row.count + column_count ||= row.size + raise IndexError, 'element size differs' unless column_count == row.size end Array.new(column_count) do |column_index| @@ -937,4 +952,8 @@ class Array end h end + + alias append push + alias prepend unshift + alias filter! select! end |
