Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/array.rb

Instance Method Summary collapse

Instance Method Details

#filter_to_type(typ) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ext/array.rb', line 16

def filter_to_type(typ)
  if typ == 'Boolean'
    compact.select { |i| i.is_a?(TrueClass) || i.is_a?(FalseClass) }
  elsif typ == 'DateTime'
    compact.select { |i| i.is_a?(Date) || i.is_a?(DateTime) || i.is_a?(Time) }
      .map { |i| i.to_datetime }
  elsif typ == 'Numeric'
    compact.select { |i| i.is_a?(Numeric) }
  elsif typ == 'String'
    map { |i| i.to_s }
  elsif typ == 'NilClass'
    self
  else
    raise ArgumentError, "cannot filter_to_type for type '#{typ}'"
  end
end

#map_booleansObject

Map booleans true to 1 and false to 0 so they can be compared in a sort with the <=> operator.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/ext/array.rb', line 4

def map_booleans
  map do |v|
    if v == true
      1
    elsif v == false
      0
    else
      v
    end
  end
end