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
7 8 9 |
# File 'lib/useful/ruby_extensions/array.rb', line 7 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
31 32 33 34 35 36 |
# File 'lib/useful/ruby_extensions/array.rb', line 31 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
22 23 24 |
# File 'lib/useful/ruby_extensions/array.rb', line 22 def merge(an_a) self.class.merge(self.dup, an_a) end |
#merge!(an_a) ⇒ Object
adds the contents of an_a to this array, removing duplicates (inline version of #merge)
26 27 28 |
# File 'lib/useful/ruby_extensions/array.rb', line 26 def merge!(an_a) self.class.merge(self, an_a) end |