Class: Array

Inherits:
Object show all
Defined in:
lib/scruby/core_ext/array.rb,
lib/scruby/core_ext/delegator_array.rb

Direct Known Subclasses

DelegatorArray, TypedArray

Instance Method Summary collapse

Instance Method Details

#collect_with_indexObject

collect with index



3
4
5
# File 'lib/scruby/core_ext/array.rb', line 3

def collect_with_index
  self.zip( (0...self.size).map ).collect{ |element, index| yield element, index }
end

#encode_floatsObject

:nodoc:



28
29
30
# File 'lib/scruby/core_ext/array.rb', line 28

def encode_floats #:nodoc:
  [self.size].pack('n') + self.pack('g*') #TODO: Deprecate
end

#peelObject



36
37
38
# File 'lib/scruby/core_ext/array.rb', line 36

def peel
  self.dup.peel! || self
end

#peel!Object



32
33
34
# File 'lib/scruby/core_ext/array.rb', line 32

def peel!
  self.replace self.first if self.first.kind_of? Array if self.size == 1
end

#to_arrayObject

Returns self



26
# File 'lib/scruby/core_ext/array.rb', line 26

def to_array; self; end

#to_daObject



32
33
34
# File 'lib/scruby/core_ext/delegator_array.rb', line 32

def to_da
  DelegatorArray.new self
end

#wrap_and_zip(*args) ⇒ Object



19
20
21
22
23
# File 'lib/scruby/core_ext/array.rb', line 19

def wrap_and_zip *args
  max  = args.map{ |a| instance_of?(Array) ? a.size : 0 }.max.max( self.size )
  args = args.collect{ |a| a.to_array.wrap_to( max ) }
  self.wrap_to( max ).zip( *args )
end

#wrap_to(size) ⇒ Object



7
8
9
10
# File 'lib/scruby/core_ext/array.rb', line 7

def wrap_to size
  return self if size == self.size
  self.dup.wrap_to! size
end

#wrap_to!(size) ⇒ Object



12
13
14
15
16
17
# File 'lib/scruby/core_ext/array.rb', line 12

def wrap_to! size
  return nil if size == self.size
  original_size = self.size
  size.times { |i| self[ i ] = self[ i % original_size ] }
  self
end