Module: Enumerable

Defined in:
lib/formatted_string/formats/xml.rb

Instance Method Summary collapse

Instance Method Details

#crawl(&block) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/formatted_string/formats/xml.rb', line 18

def crawl(&block)
  raise ArgumentError, "no block given" unless block_given?
  self.each do |v|
    k = self
    v = case block.arity
    when 1
      yield(v)
    when 2
      yield(k,v)
    when 3
      yield(self,k,v)
    end
    if v.is_a?(Array)
      v.crawl(&block)
    elsif v.is_a?(Hash)
      v.crawl(&block)
    end
  end
end

#group_byObject



11
12
13
14
15
16
# File 'lib/formatted_string/formats/xml.rb', line 11

def group_by
  inject({}) do |groups, element|
    (groups[yield(element)] ||= []) << element
    groups
  end
end