Module: MMETools::Enumerable

Extended by:
Enumerable
Included in:
Enumerable
Defined in:
lib/mme_tools/enumerable.rb

Overview

Methods for Enumerables (Arrays and other each-enabled stuff)

Instance Method Summary collapse

Instance Method Details

#classify(enumrbl, &block) ⇒ Object

Interessant iterador que classifica un enumerable (The Ruby Way , Ed. 2 - p 289)



29
30
31
32
33
34
35
36
37
# File 'lib/mme_tools/enumerable.rb', line 29

def classify(enumrbl, &block)
  hash = {}
  enumrbl.each do |el|
    res = block.call el # tb res=yield(el)
    hash[res] = [] unless hash.has_key? res
    hash[res] << el
  end
  hash
end

#compose(*enumerables) ⇒ Object

torna un array on cada element es una tupla formada per un element de cada enumerable. Si se li passa un bloc se li passa al bloc cada tupla i el resultat del bloc s’emmagatzema a l’array tornat.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mme_tools/enumerable.rb', line 15

def compose(*enumerables)
  res=[]
  enumerables.map(&:size).max.times do
    tupla=[]
    for enumerable in enumerables
      tupla << enumerable.shift
    end
    res << (block_given? ? yield(tupla) : tupla)
  end
  res
end

#even_values(array) ⇒ Object



81
82
83
84
# File 'lib/mme_tools/enumerable.rb', line 81

def even_values(array)
  array.values_at(* array.each_index.select {|i| i.even?})
  # array.select_with_index{|item, i| item if i % 2 == 1}
end

#from_to(first, last, options = nil) ⇒ Object

FIXME I don’t know really why I designed this … possibly drop candidate returns an array containing from first to last options is a hash that can contain: :comp=>eq_method is a a symbol with the name of the method that sent to

an element with another element as parameter evaluates equality. If not
supplied +:==+ assumed.

:max=>max_num is the maximum size of the returned array. If not supplied

false assumed (no limit)

:last_included?=>true or false tells if last should be included.

If not included +true+ assumed

The code block is not optional: it is passed an element and should return the next.



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

def from_to(first, last, options=nil)
  if options && options.is_a?(Hash)
    maxcount = options.has_key?(:max) ? options[:max] : false
    lastincluded = options.has_key?(:last_included?) ? options[:last_included?] : true
  else
    maxcount = false
    lastincluded = true
  end
  ret = [first]
  count = 1
  while true
    first = yield(first)
    if first == last or (maxcount ? (count > maxcount) : false)
      ret << first if lastincluded
      break
    end
    ret << first
  end
  ret
end

#odd_values(array) ⇒ Object

torna un array amb els elements parells mes a stackoverflow.com/questions/1614147/odd-or-even-entries-in-a-ruby-array



74
75
76
77
# File 'lib/mme_tools/enumerable.rb', line 74

def odd_values(array)
  array.values_at(* array.each_index.select {|i| i.odd?})
  # array.select_with_index{|item, i| item if i % 2 == 1}
end