Module: Useful::RubyExtensions::Array
- Included in:
- Array
- Defined in:
- lib/useful/ruby_extensions/array.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#groups(size = 1) ⇒ Object
(also: #/, #chunks)
split into an array of sized-arrays.
-
#merge(an_a) ⇒ Object
returns a new array, containing the contents of an_a with the contents of this array, removing duplicates.
-
#merge!(an_a) ⇒ Object
adds the contents of an_a to this array, removing duplicates (inline version of #merge).
Class Method Details
.included(klass) ⇒ Object
6 7 8 |
# File 'lib/useful/ruby_extensions/array.rb', line 6 def self.included(klass) klass.extend(ClassMethods) if klass.kind_of?(Class) end |
Instance Method Details
#groups(size = 1) ⇒ Object Also known as: /, chunks
split into an array of sized-arrays
30 31 32 33 34 35 |
# File 'lib/useful/ruby_extensions/array.rb', line 30 def groups(size=1) return [] if size <= 0 n,r = self.size.divmod(size) grps = (0..(n-1)).collect{|i| self[i*size,size]} r > 0 ? grps << self[-r,r] : grps end |
#merge(an_a) ⇒ Object
returns a new array, containing the contents of an_a with the contents of this array, removing duplicates
21 22 23 |
# File 'lib/useful/ruby_extensions/array.rb', line 21 def merge(an_a) self.class.merge(self.clone, an_a) end |
#merge!(an_a) ⇒ Object
adds the contents of an_a to this array, removing duplicates (inline version of #merge)
25 26 27 |
# File 'lib/useful/ruby_extensions/array.rb', line 25 def merge!(an_a) self.class.merge(self, an_a) end |