Class: Array

Defined in:
lib/plist4r/mixin/ruby_stdlib.rb

Instance Methods

Instance Method Details

- (true, false) multidim?

And array is considered multi-dimensional if all of the first-order elements are also arrays.

Examples:

[[1],[2],[3]].multidim?
=> true

[[1],2,[3]].multidim?
=> false

Returns:

  • (true, false)

    true for a Multi-Dimensional array, false otherwise



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plist4r/mixin/ruby_stdlib.rb', line 36

def multidim?
  case self.size
  when 0
    false
  else
    each do |e|
      return false unless e.class == Array
    end
    true
  end
end

- (Object) to_ranges

Converts an array of values (which must respond to #succ) to an array of ranges. For example,

Examples:

[3,4,5,1,6,9,8].to_ranges => [1,3..6,8..9] 


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/plist4r/mixin/ruby_stdlib.rb', line 51

def to_ranges
  array = self.compact.uniq.sort
  ranges = []
  if !array.empty?
    # Initialize the left and right endpoints of the range
    left, right = array.first, nil
    array.each do |obj|
      # If the right endpoint is set and obj is not equal to right's successor 
      # then we need to create a range.
      if right && obj != right.succ
        ranges << Range.new(left,right)
        left = obj
      end
      right = obj
    end
    ranges << Range.new(left,right)
  end
  ranges
end