Module: SugarUtils

Defined in:
lib/sugar_utils.rb,
lib/sugar_utils/file.rb,
lib/sugar_utils/version.rb

Defined Under Namespace

Modules: File

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Class Method Summary collapse

Class Method Details

.ensure_boolean(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/sugar_utils.rb', line 10

def self.ensure_boolean(value)
  return false if value.respond_to?(:to_s) && value.to_s.casecmp('false').zero?
  value ? true : false
end

.ensure_integer(value) ⇒ Integer

Parameters:

  • value (String, Float, Integer)

Returns:

  • (Integer)

Raises:

  • (ArgumentError)

    if the value is a string which cannot be converted

  • (TypeError)

    if value is type which cannot be converted



21
22
23
24
25
# File 'lib/sugar_utils.rb', line 21

def self.ensure_integer(value)
  return value      if value.is_a?(Integer)
  return value.to_i if value.is_a?(Float)
  Float(value).to_i
end