Module: Unbounded::Utility

Extended by:
ActiveSupport::Concern, Utility
Included in:
Unbounded, Formats, Utility
Defined in:
lib/unbounded/utility.rb

Overview

Shared utility methods

Instance Method Summary collapse

Instance Method Details

#match_to_hash(match) ⇒ Hash

Convert matchdata to a hash.

Parameters:

  • match (MatchData)

Returns:

  • (Hash)


11
12
13
# File 'lib/unbounded/utility.rb', line 11

def match_to_hash(match)
  Hash[match.names.zip(match.captures)]
end

#numerify(thing, default_value = nil) ⇒ Numeric, ...

Parameters:

  • thing (Object)

    something to numerify

  • default_value (Object) (defaults to: nil)

    a value to return if thing fails to numerify. If it is :original, it will return the value of thing as-is.

Returns:

  • (Numeric, Object, nil)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/unbounded/utility.rb', line 19

def numerify(thing, default_value = nil)
  return thing if thing.is_a?(Numeric)

  if thing.respond_to?(:include?) && thing.include?('.')
    Float(thing)
  else
    Integer(thing)
  end
rescue ArgumentError, TypeError
  if default_value == :original
    thing
  else
    default_value
  end
end