Module: Flattenum

Defined in:
lib/flattenum.rb

Class Method Summary collapse

Class Method Details

.flattenum(thing) ⇒ Object Also known as: this



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flattenum.rb', line 10

def flattenum(thing)
  return enum_for(__callee__, thing) unless block_given?

  if ::Flattenum.should_flatten?(thing)
    thing.each do |potential_object|
      flattenum(potential_object).each do |object|
        yield object
      end
    end

  else
    yield thing
  end
end

.should_flatten?(thing) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/flattenum.rb', line 5

def self.should_flatten?(thing)
  thing.is_a? Array or thing.is_a? Enumerator
end