Class: TypeChecker

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

Instance Method Summary collapse

Instance Method Details

#is_any?(thing) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/typechecker.rb', line 47

def is_any?(thing) 
  return false if is_nil?(thing)      
  return true if is_integer?(thing) 
  return true if is_float?(thing)
  return true if is_space?(thing)
  true if thing.is_a? String
end

#is_date?(data, format) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/typechecker.rb', line 19

def is_date?(data, format)
  parsed = nil
  begin
    parsed = DateTime.parse(data)
  rescue
  end
  output = parsed.strftime(format) if parsed
  matches = (output == data)

  return matches
end

#is_float?(str) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'lib/typechecker.rb', line 11

def is_float?(str)
  str = str.to_s
  str.strip!

  match_pattern = /^[-+]?[0-9]*\.[0-9]+?$/
  return !str.match(match_pattern).nil?
end

#is_integer?(str) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
# File 'lib/typechecker.rb', line 3

def is_integer?(str)
  str = str.to_s
  str.strip!

  match_pattern = /^[-+]?[0-9][0-9]*?$/
  return !str.match(match_pattern).nil?
end

#is_nil?(thing) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/typechecker.rb', line 35

def is_nil?(thing) 
  return thing.nil?
end

#is_space?(thing) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/typechecker.rb', line 31

def is_space?(thing)
  return thing.to_s == " "
end

#is_string?(thing) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/typechecker.rb', line 39

def is_string?(thing) 
  return false if is_nil?(thing)      
  return false if is_integer?(thing) 
  return false if is_float?(thing)
  return true if is_space?(thing)
  true if thing.is_a? String
end