Module: Enumerable

Included in
Array
Defined in
lib/kyanite/enumerable/structure.rb,
lib/kyanite/enumerable/structure.rb,
lib/kyanite/enumerable/enumerable_enumerables.rb

Enumerable Additions

Kyanite definitions

Enumerable

Kyanite tests and examples

TestKyaniteEnumerableStructure

Usage

require 'kyanite/enumerable/structure'

Structure Reflection (collapse)

Instance Method Summary (collapse)

Instance Method Details

- (Object) contentclass(options = {})

What kind of objects contains the collection? Returns the class of content elements, or Object if there are several.

Parameters is the accuracy with which the content is checked.

:precision => 1

only the first element is checked

:precision => 2

the first and the last elements are checked (STANDARD)

:precision => :all

every element is checked

:ignore_nil => true

NilClass will not be listed (STANDARD)

:ignore_nil => false

NilClass will be listed

Tests and examples here.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kyanite/enumerable/structure.rb', line 102

def contentclass( options={} )
  precision  = options[:precision]    || 2
  ignore_nil = options[:ignore_nil];  ignore_nil = true   if ignore_nil.nil?
  return nil   if  self.empty? 
  case precision
  
  when 1
    result = self.first.class
    if ( result == NilClass  &&  ignore_nil )
      return self.compact.contentclass( :precision => precision, :ignore_nil => false  )
    else
      return result
    end
    
  when 2
    f = self.first.class    
    l = self.last.class   
    if ( (f == NilClass || l == NilClass) &&  ignore_nil )
      return self.compact.contentclass( :precision => precision, :ignore_nil => false  )
    end      
    if f == l      
      return f
    else
      result = f.ancestors & l.ancestors
      #see result - [Object, Kernel, Precision, Perception::NumericI,  PP::ObjectMixin,  KKernel]
      return Numeric      if result.include?(Numeric)
      return Enumerable   if result.include?(Enumerable)
      return Object
    end
    
  when :all
    unless ( self.kind_of?(Hash) || self.kind_of?(Hashery::Dictionary) )
      c = self.collect {|e| e.class}
    else
      c = self.collect {| key, value | value.class}      
    end 
    c = c - [NilClass]  if ignore_nil
    c.uniq!  
    if c.empty? 
      return nil       if ignore_nil
      return NilClass   
    end
    return c[0]       if c.size == 1 
    result = c[0].ancestors
    c[1..-1].each do |e|
      result = result & e.ancestors
    end
    #see result - [Object, Kernel, Precision, Perception::NumericI,  PP::ObjectMixin,  KKernel]      
    return Numeric      if result.include?(Numeric)
    return Enumerable   if result.include?(Enumerable)
    return Object

  else # case precision
    raise ArgumentError, ':precision should be 1, 2 or :all'
  end #case
  
end

- (Array) distribution(mode = :size)

Returns the distribution of size, class or any other characteristics of the enumerated elements. See also Hash#distribution. Tests and examples here.

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kyanite/enumerable/structure.rb', line 75

def distribution( mode = :size)
  verteilung = Hash.new
  each do | element |
    value = element.respond(mode)
    if verteilung.has_key?(value)
      verteilung[value] += 1
    else
      verteilung[value] = 1
    end # if      
  end #each
  verteilung.to_a.sort   
end

- (true) is_collection?

-- Defined for all objects: Do I contain multiple objects? String and Range are not considered as collection.

Tests and examples here. true -- Defined for all objects: Do I contain multiple objects? String and Range are not considered as collection.

Tests and examples here.

Returns:

  • (true)


61
# File 'lib/kyanite/enumerable/structure.rb', line 61

def is_collection?;                 true;          end

- (Object) transpose!

In-place-variant of transpose.



12
13
14
# File 'lib/kyanite/enumerable/enumerable_enumerables.rb', line 12

def transpose!
    self.replace(self.transpose)
end